@vuepress/markdown 1.4.0 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/lib/snippet.js +103 -9
  2. package/package.json +4 -8
  3. package/LICENSE +0 -21
package/lib/snippet.js CHANGED
@@ -1,5 +1,74 @@
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()
@@ -7,15 +76,32 @@ module.exports = function snippet (md, options = {}) {
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vuepress/markdown",
3
- "version": "1.4.0",
3
+ "version": "1.5.2",
4
4
  "description": "markdown for vuepress",
5
5
  "keywords": [
6
6
  "documentation",
@@ -9,7 +9,7 @@
9
9
  "vue",
10
10
  "vuepress"
11
11
  ],
12
- "homepage": "https://github.com/vuejs/vuepress/packages/@vuepress/markdown#readme",
12
+ "homepage": "https://github.com/vuejs/vuepress/blob/master/packages/@vuepress/markdown#readme",
13
13
  "bugs": {
14
14
  "url": "https://github.com/vuejs/vuepress/issues"
15
15
  },
@@ -22,7 +22,7 @@
22
22
  "author": "Evan You",
23
23
  "main": "index.js",
24
24
  "dependencies": {
25
- "@vuepress/shared-utils": "^1.4.0",
25
+ "@vuepress/shared-utils": "1.5.2",
26
26
  "markdown-it": "^8.4.1",
27
27
  "markdown-it-anchor": "^5.0.2",
28
28
  "markdown-it-chain": "^1.3.0",
@@ -30,9 +30,6 @@
30
30
  "markdown-it-table-of-contents": "^0.4.0",
31
31
  "prismjs": "^1.13.0"
32
32
  },
33
- "devDependencies": {
34
- "@vuepress/test-utils": "^1.4.0"
35
- },
36
33
  "publishConfig": {
37
34
  "access": "public"
38
35
  },
@@ -41,6 +38,5 @@
41
38
  "name": "ULIVZ",
42
39
  "email": "chl814@foxmail.com"
43
40
  }
44
- ],
45
- "gitHead": "58ed07f013b7f7768094e57314f993c39dc6750e"
41
+ ]
46
42
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2018-present, Yuxi (Evan) You
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.