forked from Qard/remark-include
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (59 loc) · 1.86 KB
/
index.js
File metadata and controls
76 lines (59 loc) · 1.86 KB
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
/* eslint-disable max-statements, func-names */
let path = require('path')
let rebuild = require('./rebuild.js')
let walkers = require('./walkers.js')
const zero = 0
const one = 1
const PLUGIN_NAME = 'remark-import'
function bumpAndLink (root, headerLevel, child) {
root = rebuild.headings(root, headerLevel)
root = rebuild.links(root, child.value.replace(/"/g, ''), child.source.dirname)
return root
}
function splitAndMerge (children, i, root) {
let head = children.slice(zero, i)
let tail = children.slice(i + one)
children = head.concat(root.children)
.concat(tail)
return children
}
function tokenizer (eat, value, silent) {
let parseImport = /^@import (.*?)(\n|$)/
let node
while (parseImport.test(value)) {
let file = value.match(parseImport)[1]
let frag = '@import ' + file
value = value.slice(frag.length)
eat(frag)({
source : this.file,
type : 'import',
value : file
})
}
return node
}
module.exports = function (options) {
var proc = this
var prt = proc.Parser.prototype
prt.blockTokenizers.import = tokenizer
prt.blockMethods.unshift('import')
return function transformer (ast, file) {
let children = ast.children
var headerLevel = 0
for (let i = 0; i < children.length; i++) {
let child = children[i]
if (child.type === 'import') {
let parsedImport = proc.parse(
walkers.toFile(path.join(child.source.dirname, child.value.replace(/"/g, '')))
)
file.info(`Importing ${child.value} from @import statement.`, child.position, PLUGIN_NAME)
let root = proc.runSync(parsedImport)
root = bumpAndLink(root, headerLevel, child)
children = splitAndMerge(children, i, root)
i += root.children.length - one
}
if (child.type === 'heading') headerLevel = child.depth
}
ast.children = children
}
}