-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
156 lines (136 loc) · 3.93 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
149
150
151
152
153
154
155
156
'use strict'
const Plugin = require('markdown-it-regexp')
const extend = require('extend')
const sanitize = require('sanitize-filename')
const Url = require('reurl').RawUrl
const INITIAL_HASH_REGEX = /^#/
const DIR_SEPARATOR_REGEX = /[/\\]/g
module.exports = (options) => {
const defaults = {
baseURL: '/',
relativeBaseURL: './',
makeAllLinksAbsolute: false,
uriSuffix: '.html',
htmlAttributes: {
},
generatePagePathFromLabel: (label) => {
return label
},
postProcessPagePath: (pagePath) => {
pagePath = pagePath.trim()
pagePath = pagePath.split('/').map(sanitize).join('/')
pagePath = pagePath.replace(/\s+/g, '_')
return pagePath
},
postProcessPageHash: (pageHash) => {
pageHash = pageHash.trim()
pageHash = pageHash.split('/').map(sanitize).join('/')
pageHash = pageHash.replace(/\s+/g, '_')
return pageHash
},
postProcessLabel: (label) => {
label = label.trim()
if (label.match(INITIAL_HASH_REGEX)) {
label = label.replace(INITIAL_HASH_REGEX, '')
}
else {
label = label.replace(/#[^#]*$/, '')
}
return label
}
}
options = extend(true, defaults, options)
function isAbsolute(pagePath) {
return options.makeAllLinksAbsolute || pagePath.charCodeAt(0) === 0x2F/* / */
}
/**
* Converts a path from the form '/path/to'
* to an array of the form ['path', 'to']
*/
function pathStrToArray(pathStr) {
return (
pathStr.split(DIR_SEPARATOR_REGEX)
.filter(dirName => !!dirName)
)
}
return Plugin(
/\[\[([^|\]\n]+)(\|([^\]\n]+))?\]\]/,
(match, utils) => {
let label = ''
let pagePath = null
let htmlAttrs = []
let htmlAttrsString = ''
const absoluteBaseDirs = pathStrToArray(options.baseURL)
const relativeBaseDirs = pathStrToArray(options.relativeBaseURL)
const postProcessLabel = options.postProcessLabel
const postProcessPagePath = (
options.postProcessPageName ||
options.postProcessPagePath
)
const postProcessPageHash = options.postProcessPageHash
const generatePagePathFromLabel = (
options.generatePageNameFromLabel ||
options.generatePagePathFromLabel
)
const isSplit = !!match[3]
if (isSplit) {
label = match[3]
pagePath = match[1]
}
else {
label = match[1]
pagePath = generatePagePathFromLabel(match[1])
}
// parse page path
let pageUrl = new Url(pagePath)
// run postprocessing
if (pageUrl.file) {
const file = postProcessPagePath(pageUrl.file)
pageUrl = pageUrl.set({
file
})
}
if (pageUrl.hash) {
const hash = postProcessPageHash(pageUrl.hash)
pageUrl = pageUrl.set({
hash
})
}
label = postProcessLabel(label)
if (isAbsolute(pagePath)) {
const dirs = absoluteBaseDirs.concat(
pageUrl.dirs || []
)
pageUrl = pageUrl.set({
root: '/',
dirs,
})
}
else if(pageUrl.file) { // relative
const dirs = relativeBaseDirs.concat(
pageUrl.dirs || []
)
pageUrl = pageUrl.set({
root: null,
dirs,
})
}
// add the URI suffix (e.g. `.html`)
// but only if the filename is not empty
if (pageUrl.file && options.uriSuffix) {
pageUrl = pageUrl.set({
file: pageUrl.file + options.uriSuffix
})
}
const href = pageUrl.toString()
const escapedHref = utils.escape(href)
htmlAttrs.push(`href="${escapedHref}"`)
for (let attrName in options.htmlAttributes) {
const attrValue = options.htmlAttributes[attrName]
htmlAttrs.push(`${attrName}="${attrValue}"`)
}
htmlAttrsString = htmlAttrs.join(' ')
return `<a ${htmlAttrsString}>${label}</a>`
}
)
}