-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (132 loc) · 4.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const fs = require(`fs`)
const path = require(`path`)
const visit = require('unist-util-visit')
const normalize = require('normalize-path')
const rangeParser = require(`parse-numeric-range`)
const EXT_TO_LANG_MAP = {
md: `markup`,
js: `jsx`,
rb: `ruby`,
ps1: `powershell`,
sh: `bash`,
bat: `batch`,
py: `python`,
psm1: `powershell`,
tex: `latex`,
h: `c`,
}
const getFileLang = (file) => {
if (!file.includes(`.`)) {
return null
}
const extension = file.split(`.`).pop().toLowerCase()
const lang = EXT_TO_LANG_MAP.hasOwnProperty(extension)
? EXT_TO_LANG_MAP[extension]
: extension.toLowerCase()
return lang
}
const embedCode = (node, directory, fileName) => {
let filePath = normalize(path.join(directory, fileName))
let lines = []
let sname = ``
const rangePrefixIndex = filePath.indexOf(`#L`)
if (rangePrefixIndex > -1) {
const range = filePath.slice(rangePrefixIndex + 2)
if (range.length === 1) {
lines = [Number.parseInt(range, 10)]
} else {
lines = rangeParser(range)
}
// Remove everything after the range prefix from file path
filePath = filePath.slice(0, rangePrefixIndex)
} else {
// Check to see if there is a {snippet: "snippetName"} following the file path.
// This syntax could support additional options in the future - for now, only
// handle a string that contains a `snippet :` option.
const optionIndex = filePath.indexOf(`{`)
if (optionIndex > -1) {
const optionStr = filePath.slice(optionIndex)
filePath = filePath.slice(0, optionIndex)
try {
const optVal = JSON.parse(
optionStr.replace(/snippet\s*:/, `"snippet":`)
)
if (
typeof optVal != `undefined` &&
typeof optVal.snippet != `undefined`
) {
sname = optVal.snippet
} else {
throw Error(`Invalid snippet options specified: ${optionStr}`)
}
} catch (err) {
throw Error(`Invalid snippet options specified: ${optionStr}`)
}
}
}
if (!fs.existsSync(filePath)) {
throw Error(`Invalid snippet specified; no such file "${filePath}"
console.log('파일경로', ${directory}, ${fileName}, ${filePath})
`)
}
let code = fs.readFileSync(filePath, 'utf8').trim()
if (lines.length) {
code = code
.split(`\n`)
.filter((_, lineNumber) => lines.includes(lineNumber + 1))
.join(`\n`)
} else if (sname.length) {
const startSnippetMatcher = new RegExp(
`start-snippet{${sname}}[^\r\n]*[\r\n](.*)`,
`gs`
)
const startSnippetMatch = startSnippetMatcher.exec(code)
if (startSnippetMatch && startSnippetMatch.length >= 2) {
code = startSnippetMatch[1]
const endSnippetMatcher = new RegExp(
`(.*)[\r\n][^\r\n]*end-snippet{${sname}}`,
`gs`
)
const endSnippetMatch = endSnippetMatcher.exec(code)
if(endSnippetMatch && endSnippetMatch.length >= 2) {
code = endSnippetMatch[1]
}
} else {
code = ``
}
}
const lang = getFileLang(filePath)
node.type = 'code'
node.value = code
node.lang = lang
}
module.exports = (options) => {
return async (tree, file) => {
const specifiedDir = options.directory
const directory = specifiedDir ? specifiedDir : path.dirname(file.path)
try {
if (!fs.existsSync(directory)) {
throw Error(`Invalid directory specified "${directory}"`)
}
visit(tree, (node) => {
const { value } = node
if (value && value.includes(`<!-- embed:`)) {
const start = value.indexOf('<!-- embed:') + 11,
end = value.indexOf(' -->'),
fileName = value.substring(start, end)
embedCode(node, directory, fileName)
}
})
visit(tree, 'inlineCode', (node) => {
const { value } = node
if (value.startsWith(`embed:`)) {
const fileName = value.substring(6)
embedCode(node, directory, fileName)
}
})
} catch (error) {
console.log(error)
throw error
}
}
}