|
1 | 1 | const { fs, logger, path } = require('@vuepress/shared-utils')
|
2 | 2 |
|
| 3 | +function dedent (text) { |
| 4 | + const wRegexp = /^([ \t]*)(.*)\n/gm |
| 5 | + let match; let minIndentLength = null |
| 6 | + |
| 7 | + while ((match = wRegexp.exec(text)) !== null) { |
| 8 | + const [indentation, content] = match.slice(1) |
| 9 | + if (!content) continue |
| 10 | + |
| 11 | + const indentLength = indentation.length |
| 12 | + if (indentLength > 0) { |
| 13 | + minIndentLength |
| 14 | + = minIndentLength !== null |
| 15 | + ? Math.min(minIndentLength, indentLength) |
| 16 | + : indentLength |
| 17 | + } else break |
| 18 | + } |
| 19 | + |
| 20 | + if (minIndentLength) { |
| 21 | + text = text.replace( |
| 22 | + new RegExp(`^[ \t]{${minIndentLength}}(.*)`, 'gm'), |
| 23 | + '$1' |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + return text |
| 28 | +} |
| 29 | + |
| 30 | +function testLine (line, regexp, regionName, end = false) { |
| 31 | + const [full, tag, name] = regexp.exec(line.trim()) || [] |
| 32 | + |
| 33 | + return ( |
| 34 | + full |
| 35 | + && tag |
| 36 | + && name === regionName |
| 37 | + && tag.match(end ? /^[Ee]nd ?[rR]egion$/ : /^[rR]egion$/) |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +function findRegion (lines, regionName) { |
| 42 | + const regionRegexps = [ |
| 43 | + /^\/\/ ?#?((?:end)?region) ([\w*-]+)$/, // javascript, typescript, java |
| 44 | + /^\/\* ?#((?:end)?region) ([\w*-]+) ?\*\/$/, // css, less, scss |
| 45 | + /^#pragma ((?:end)?region) ([\w*-]+)$/, // C, C++ |
| 46 | + /^<!-- #?((?:end)?region) ([\w*-]+) -->$/, // HTML, markdown |
| 47 | + /^#((?:End )Region) ([\w*-]+)$/, // Visual Basic |
| 48 | + /^::#((?:end)region) ([\w*-]+)$/, // Bat |
| 49 | + /^# ?((?:end)?region) ([\w*-]+)$/ // C#, PHP, Powershell, Python, perl & misc |
| 50 | + ] |
| 51 | + |
| 52 | + let regexp = null |
| 53 | + let start = -1 |
| 54 | + |
| 55 | + for (const [lineId, line] of lines.entries()) { |
| 56 | + if (regexp === null) { |
| 57 | + for (const reg of regionRegexps) { |
| 58 | + if (testLine(line, reg, regionName)) { |
| 59 | + start = lineId + 1 |
| 60 | + regexp = reg |
| 61 | + break |
| 62 | + } |
| 63 | + } |
| 64 | + } else if (testLine(line, regexp, regionName, true)) { |
| 65 | + return { start, end: lineId, regexp } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return null |
| 70 | +} |
| 71 | + |
3 | 72 | module.exports = function snippet (md, options = {}) {
|
4 | 73 | const fence = md.renderer.rules.fence
|
5 | 74 | const root = options.root || process.cwd()
|
6 | 75 |
|
7 | 76 | md.renderer.rules.fence = (...args) => {
|
8 | 77 | const [tokens, idx, , { loader }] = args
|
9 | 78 | const token = tokens[idx]
|
10 |
| - const { src } = token |
| 79 | + const [src, regionName] = token.src ? token.src.split('#') : [''] |
11 | 80 | if (src) {
|
12 | 81 | if (loader) {
|
13 | 82 | loader.addDependency(src)
|
14 | 83 | }
|
15 |
| - if (fs.existsSync(src)) { |
16 |
| - token.content = fs.readFileSync(src, 'utf8') |
| 84 | + const isAFile = fs.lstatSync(src).isFile() |
| 85 | + if (fs.existsSync(src) && isAFile) { |
| 86 | + let content = fs.readFileSync(src, 'utf8') |
| 87 | + |
| 88 | + if (regionName) { |
| 89 | + const lines = content.split(/\r?\n/) |
| 90 | + const region = findRegion(lines, regionName) |
| 91 | + |
| 92 | + if (region) { |
| 93 | + content = dedent( |
| 94 | + lines |
| 95 | + .slice(region.start, region.end) |
| 96 | + .filter(line => !region.regexp.test(line.trim())) |
| 97 | + .join('\n') |
| 98 | + ) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + token.content = content |
17 | 103 | } else {
|
18 |
| - token.content = `Code snippet path not found: ${src}` |
| 104 | + token.content = isAFile ? `Code snippet path not found: ${src}` : `Invalid code snippet option` |
19 | 105 | token.info = ''
|
20 | 106 | logger.error(token.content)
|
21 | 107 | }
|
@@ -44,15 +130,23 @@ module.exports = function snippet (md, options = {}) {
|
44 | 130 |
|
45 | 131 | const start = pos + 3
|
46 | 132 | const end = state.skipSpacesBack(max, pos)
|
47 |
| - const rawPath = state.src.slice(start, end).trim().replace(/^@/, root) |
48 |
| - const filename = rawPath.split(/{/).shift().trim() |
49 |
| - const meta = rawPath.replace(filename, '') |
| 133 | + |
| 134 | + /** |
| 135 | + * raw path format: "/path/to/file.extension#region {meta}" |
| 136 | + * where #region and {meta} are optionnal |
| 137 | + * |
| 138 | + * captures: ['/path/to/file.extension', 'extension', '#region', '{meta}'] |
| 139 | + */ |
| 140 | + const rawPathRegexp = /^(.+(?:\.([a-z]+)))(?:(#[\w-]+))?(?: ?({\d+(?:[,-]\d+)?}))?$/ |
| 141 | + |
| 142 | + const rawPath = state.src.slice(start, end).trim().replace(/^@/, root).trim() |
| 143 | + const [filename = '', extension = '', region = '', meta = ''] = (rawPathRegexp.exec(rawPath) || []).slice(1) |
50 | 144 |
|
51 | 145 | state.line = startLine + 1
|
52 | 146 |
|
53 | 147 | const token = state.push('fence', 'code', 0)
|
54 |
| - token.info = filename.split('.').pop() + meta |
55 |
| - token.src = path.resolve(filename) |
| 148 | + token.info = extension + meta |
| 149 | + token.src = path.resolve(filename) + region |
56 | 150 | token.markup = '```'
|
57 | 151 | token.map = [startLine, startLine + 1]
|
58 | 152 |
|
|
0 commit comments