forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed.js
More file actions
214 lines (192 loc) · 6.12 KB
/
embed.js
File metadata and controls
214 lines (192 loc) · 6.12 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { stripIndent } from 'common-tags';
import { get } from '../util/ajax.js';
const cached = {};
/**
* Extracts the content between matching fragment markers in the text.
*
* Supported markers:
* - ### [fragment] ... ### [fragment]
* - /// [fragment] ... /// [fragment]
*
* @param {string} text - The input text that may contain embedded fragments.
* @param {string} fragment - The fragment identifier to search for.
* @returns {string} - The extracted and demented content, or an empty string if not found.
*/
function extractFragmentContent(text, fragment) {
if (!fragment) {
return text;
}
const pattern = new RegExp(
`(?:###|\\/\\/\\/)\\s*\\[${fragment}\\]([\\s\\S]*?)(?:###|\\/\\/\\/)\\s*\\[${fragment}\\]`,
);
const match = text.match(pattern);
return stripIndent((match || [])[1] || '').trim();
}
function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
let token;
let step = 0;
let count = 0;
if (!embedTokens.length) {
return cb({});
}
while ((token = embedTokens[step++])) {
const currentToken = token;
// eslint-disable-next-line no-loop-func
const next = text => {
let embedToken;
if (text) {
if (currentToken.embed.type === 'markdown') {
let path = currentToken.embed.url.split('/');
path.pop();
path = path.join('/');
// Resolves relative links to absolute
text = text.replace(/\[([^[\]]+)\]\(([^)]+)\)/g, x => {
const linkBeginIndex = x.indexOf('(');
if (x.slice(linkBeginIndex, linkBeginIndex + 2) === '(.') {
return (
x.substring(0, linkBeginIndex) +
`(${window.location.protocol}//${window.location.host}${path}/` +
x.substring(linkBeginIndex + 1, x.length - 1) +
')'
);
}
return x;
});
// This may contain YAML front matter and will need to be stripped.
const frontMatterInstalled =
($docsify.frontMatter || {}).installed || false;
if (frontMatterInstalled === true) {
text = $docsify.frontMatter.parseMarkdown(text);
}
if (currentToken.embed.fragment) {
text = extractFragmentContent(text, currentToken.embed.fragment);
}
embedToken = compile.lexer(text);
} else if (currentToken.embed.type === 'code') {
if (currentToken.embed.fragment) {
text = extractFragmentContent(text, currentToken.embed.fragment);
}
embedToken = compile.lexer(
'```' +
currentToken.embed.lang +
'\n' +
text.replace(/`/g, '@DOCSIFY_QM@') +
'\n```\n',
);
} else if (currentToken.embed.type === 'mermaid') {
embedToken = [
{
type: 'html',
text: /* html */ `<div class="mermaid">\n${text}\n</div>`,
},
];
embedToken.links = {};
} else {
embedToken = [{ type: 'html', text }];
embedToken.links = {};
}
}
cb({
token: currentToken,
embedToken,
rowIndex: currentToken.rowIndex,
cellIndex: currentToken.cellIndex,
tokenRef: currentToken.tokenRef,
});
if (++count >= embedTokens.length) {
cb({});
}
};
if (token.embed.url) {
get(token.embed.url).then(next);
} else {
next(token.embed.html);
}
}
}
export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
const hit = cached[raw];
if (hit) {
const copy = hit.slice();
copy.links = hit.links;
return done(copy);
}
const compile = compiler._marked;
let tokens = compile.lexer(raw);
const embedTokens = [];
const linkRE = compile.Lexer.rules.inline.normal.link;
const links = tokens.links;
const linkMatcher = new RegExp(linkRE.source, 'g');
tokens.forEach((token, index) => {
if (token.type === 'paragraph') {
token.text = token.text.replace(
linkMatcher,
(src, filename, href, title) => {
const embed = compiler.compileEmbed(href, title);
if (embed) {
embedTokens.push({
index,
tokenRef: token,
embed,
});
}
return src;
},
);
} else if (token.type === 'table') {
token.rows.forEach((row, rowIndex) => {
row.forEach((cell, cellIndex) => {
cell.text = cell.text.replace(
linkMatcher,
(src, filename, href, title) => {
const embed = compiler.compileEmbed(href, title);
if (embed) {
embedTokens.push({
index,
tokenRef: token,
rowIndex,
cellIndex,
embed,
});
}
return src;
},
);
});
});
}
});
// keep track of which tokens have been embedded so far
// so that we know where to insert the embedded tokens as they
// are returned
const moves = [];
walkFetchEmbed(
{ compile, embedTokens, fetch },
({ embedToken, token, rowIndex, cellIndex, tokenRef }) => {
if (token) {
if (typeof rowIndex === 'number' && typeof cellIndex === 'number') {
const cell = tokenRef.rows[rowIndex][cellIndex];
cell.embedTokens = embedToken;
} else {
// iterate through the array of previously inserted tokens
// to determine where the current embedded tokens should be inserted
let index = token.index;
moves.forEach(pos => {
if (index > pos.start) {
index += pos.length;
}
});
Object.assign(links, embedToken.links);
tokens = tokens
.slice(0, index)
.concat(embedToken, tokens.slice(index + 1));
moves.push({ start: index, length: embedToken.length - 1 });
}
} else {
cached[raw] = tokens.concat();
tokens.links = cached[raw].links = links;
done(tokens);
}
},
);
}