@rspress/core 2.0.0-beta.22 → 2.0.0-beta.24
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.
- package/dist/208.js +0 -2
- package/dist/309.js +0 -2
- package/dist/94.js +0 -2
- package/dist/cli.js +1 -3
- package/dist/index.d.ts +14 -5
- package/dist/index.js +317 -367
- package/dist/loader.d.ts +3 -1
- package/dist/loader.js +3 -6
- package/dist/processor.d.ts +5 -2
- package/dist/processor.js +270 -238
- package/dist/renderPageWorker.js +2 -2
- package/dist/runtime/ssrServerEntry.js +2 -2
- package/dist/runtime.d.ts +3 -0
- package/dist/runtime.js +2 -2
- package/dist/shiki-transformers.js +13 -13
- package/package.json +7 -8
package/dist/processor.js
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
|
-
import 'module';
|
|
2
|
-
/*#__PURE__*/ import.meta.url;
|
|
3
1
|
import node_path from "node:path";
|
|
4
2
|
import { createProcessor, nodeTypes } from "@mdx-js/mdx";
|
|
5
3
|
import { extractTextAndId, getNodeAttribute, loadFrontMatter } from "@rspress/shared/node-utils";
|
|
6
4
|
import node_os from "node:os";
|
|
5
|
+
import rehype from "@shikijs/rehype";
|
|
7
6
|
import rehype_external_links from "rehype-external-links";
|
|
8
7
|
import rehype_raw from "rehype-raw";
|
|
9
8
|
import remark_gfm from "remark-gfm";
|
|
9
|
+
import { visit } from "unist-util-visit";
|
|
10
10
|
import github_slugger from "github-slugger";
|
|
11
11
|
import { headingRank } from "hast-util-heading-rank";
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
12
|
+
import { createCssVariablesTheme } from "shiki";
|
|
13
|
+
import { readFile as promises_readFile } from "node:fs/promises";
|
|
14
14
|
import { logger as logger_logger } from "@rspress/shared/logger";
|
|
15
15
|
import picocolors_0 from "picocolors";
|
|
16
|
-
import {
|
|
16
|
+
import { addLeadingSlash, addTrailingSlash, isExternalUrl, isProduction, normalizeHref, parseUrl, removeTrailingSlash, withBase } from "@rspress/shared";
|
|
17
17
|
import { visitChildren } from "unist-util-visit-children";
|
|
18
|
-
import rehype from "@shikijs/rehype";
|
|
19
|
-
import { createCssVariablesTheme } from "shiki";
|
|
20
18
|
function applyReplaceRules(code = '', replaceRules = []) {
|
|
21
19
|
let result = code;
|
|
22
20
|
for (const rule of replaceRules)result = result.replace(rule.search, rule.replace);
|
|
@@ -33,6 +31,14 @@ function slash(p) {
|
|
|
33
31
|
function normalizePath(id) {
|
|
34
32
|
return node_path.posix.normalize(isWindows ? slash(id) : id);
|
|
35
33
|
}
|
|
34
|
+
const rehypeCodeMeta = ()=>(tree)=>{
|
|
35
|
+
visit(tree, 'element', (node)=>{
|
|
36
|
+
if ('pre' === node.tagName && node.children[0]?.type === 'element' && 'code' === node.children[0].tagName) {
|
|
37
|
+
const codeNode = node.children[0];
|
|
38
|
+
codeNode.properties.metastring = codeNode.data?.meta;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
36
42
|
const rehypeHeaderAnchor = ()=>{
|
|
37
43
|
const slugger = new github_slugger();
|
|
38
44
|
return (tree)=>{
|
|
@@ -82,225 +88,6 @@ function create(node) {
|
|
|
82
88
|
]
|
|
83
89
|
};
|
|
84
90
|
}
|
|
85
|
-
const getASTNodeImport = (name, from)=>({
|
|
86
|
-
type: 'mdxjsEsm',
|
|
87
|
-
value: `import ${name} from ${JSON.stringify(from)}`,
|
|
88
|
-
data: {
|
|
89
|
-
estree: {
|
|
90
|
-
type: 'Program',
|
|
91
|
-
sourceType: 'module',
|
|
92
|
-
body: [
|
|
93
|
-
{
|
|
94
|
-
type: 'ImportDeclaration',
|
|
95
|
-
specifiers: [
|
|
96
|
-
{
|
|
97
|
-
type: 'ImportDefaultSpecifier',
|
|
98
|
-
local: {
|
|
99
|
-
type: 'Identifier',
|
|
100
|
-
name
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
],
|
|
104
|
-
source: {
|
|
105
|
-
type: 'Literal',
|
|
106
|
-
value: from,
|
|
107
|
-
raw: `${JSON.stringify(from)}`
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
]
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
const remarkBuiltin = ({ globalComponents })=>(tree)=>{
|
|
115
|
-
const demos = globalComponents.map((componentPath)=>{
|
|
116
|
-
const filename = node_path.parse(componentPath).name;
|
|
117
|
-
const componentName = filename[0].toUpperCase() + filename.slice(1);
|
|
118
|
-
return getASTNodeImport(componentName, componentPath);
|
|
119
|
-
});
|
|
120
|
-
tree.children.unshift(...demos);
|
|
121
|
-
};
|
|
122
|
-
let isLogged = false;
|
|
123
|
-
function hintRelativeMarkdownLink() {
|
|
124
|
-
if (isLogged) return;
|
|
125
|
-
isLogged = true;
|
|
126
|
-
logger_logger.info('[Rspress v2] Markdown links without "./" prefix are now relative links, e.g: [](guide/getting-started) is equal to [](./guide/getting-started). You can rewrite it to [](/guide/getting-started) to always use absolute links, or use [](./getting-started.md)');
|
|
127
|
-
}
|
|
128
|
-
function normalizeLink_checkDeadLinks(internalLinks, filePath, routeService) {
|
|
129
|
-
const errorInfos = [];
|
|
130
|
-
let possibleBreakingChange = false;
|
|
131
|
-
[
|
|
132
|
-
...internalLinks.entries()
|
|
133
|
-
].forEach(([nodeUrl, link])=>{
|
|
134
|
-
const cleanLinkPath = linkToRoutePath(link);
|
|
135
|
-
if (!cleanLinkPath) return;
|
|
136
|
-
if (!nodeUrl.startsWith('/') && /^\w/.test(nodeUrl)) possibleBreakingChange = true;
|
|
137
|
-
if (!routeService.isExistRoute(removeTrailingSlash(cleanLinkPath)) && !routeService.isExistRoute(addTrailingSlash(cleanLinkPath))) errorInfos.push([
|
|
138
|
-
nodeUrl,
|
|
139
|
-
link
|
|
140
|
-
]);
|
|
141
|
-
});
|
|
142
|
-
if (errorInfos.length > 0) {
|
|
143
|
-
if (possibleBreakingChange) hintRelativeMarkdownLink();
|
|
144
|
-
logger_logger.error(`Dead links found in ${picocolors_0.cyan(filePath)}:
|
|
145
|
-
${errorInfos.map(([nodeUrl, link])=>` ${picocolors_0.green(`"[..](${nodeUrl})"`)} ${picocolors_0.gray(link)}`).join('\n')}`);
|
|
146
|
-
if (isProduction()) throw new Error('Dead link found');
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
function normalizeLink(nodeUrl, routeService, filePath, cleanUrls, internalLinks, __base) {
|
|
150
|
-
if (!nodeUrl) return '';
|
|
151
|
-
if (nodeUrl.startsWith('#')) return nodeUrl;
|
|
152
|
-
let { url, hash } = parseUrl(nodeUrl);
|
|
153
|
-
if (isExternalUrl(url)) return url + (hash ? `#${hash}` : '');
|
|
154
|
-
if (!routeService) return nodeUrl;
|
|
155
|
-
if (url.startsWith('/')) {
|
|
156
|
-
const { routePath } = routeService.normalizeRoutePath(url);
|
|
157
|
-
url = routePath;
|
|
158
|
-
} else {
|
|
159
|
-
const anotherFileAbsolutePath = node_path.join(node_path.dirname(filePath), url);
|
|
160
|
-
url = routeService.absolutePathToRoutePath(anotherFileAbsolutePath);
|
|
161
|
-
}
|
|
162
|
-
if ('boolean' == typeof cleanUrls) url = normalizeHref(url, cleanUrls);
|
|
163
|
-
else {
|
|
164
|
-
url = normalizeHref(url, false);
|
|
165
|
-
url = url.replace(/\.html$/, cleanUrls);
|
|
166
|
-
}
|
|
167
|
-
internalLinks.set(nodeUrl, url);
|
|
168
|
-
if (hash) url += `#${hash}`;
|
|
169
|
-
if (__base) url = withBase(url, __base);
|
|
170
|
-
return url;
|
|
171
|
-
}
|
|
172
|
-
function linkToRoutePath(routePath) {
|
|
173
|
-
return decodeURIComponent(routePath.split('#')[0]).replace(/\.html$/, '').replace(/\/index$/, '/');
|
|
174
|
-
}
|
|
175
|
-
const normalizeImageUrl = (imageUrl)=>{
|
|
176
|
-
if (isExternalUrl(imageUrl) || imageUrl.startsWith('/')) return '';
|
|
177
|
-
return imageUrl;
|
|
178
|
-
};
|
|
179
|
-
const remarkPluginNormalizeLink = ({ cleanUrls, routeService, checkDeadLinks: shouldCheckDeadLinks = false, __base })=>(tree, file)=>{
|
|
180
|
-
const internalLinks = new Map();
|
|
181
|
-
visit(tree, 'link', (node)=>{
|
|
182
|
-
const { url: nodeUrl } = node;
|
|
183
|
-
const link = normalizeLink(nodeUrl, routeService, file.path, cleanUrls, internalLinks, __base);
|
|
184
|
-
node.url = link;
|
|
185
|
-
});
|
|
186
|
-
visit(tree, 'definition', (node)=>{
|
|
187
|
-
const { url: nodeUrl } = node;
|
|
188
|
-
const link = normalizeLink(nodeUrl, routeService, file.path, cleanUrls, internalLinks, __base);
|
|
189
|
-
node.url = link;
|
|
190
|
-
});
|
|
191
|
-
if (shouldCheckDeadLinks && routeService) normalizeLink_checkDeadLinks(internalLinks, file.path, routeService);
|
|
192
|
-
const images = [];
|
|
193
|
-
const getMdxSrcAttribute = (tempVar)=>({
|
|
194
|
-
type: 'mdxJsxAttribute',
|
|
195
|
-
name: 'src',
|
|
196
|
-
value: {
|
|
197
|
-
type: 'mdxJsxAttributeValueExpression',
|
|
198
|
-
value: tempVar,
|
|
199
|
-
data: {
|
|
200
|
-
estree: {
|
|
201
|
-
type: 'Program',
|
|
202
|
-
sourceType: 'module',
|
|
203
|
-
body: [
|
|
204
|
-
{
|
|
205
|
-
type: 'ExpressionStatement',
|
|
206
|
-
expression: {
|
|
207
|
-
type: 'Identifier',
|
|
208
|
-
name: tempVar
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
]
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
visit(tree, 'image', (node)=>{
|
|
217
|
-
const { alt, url } = node;
|
|
218
|
-
if (!url) return;
|
|
219
|
-
const imagePath = normalizeImageUrl(url);
|
|
220
|
-
if (!imagePath) return;
|
|
221
|
-
const tempVariableName = `image${images.length}`;
|
|
222
|
-
Object.assign(node, {
|
|
223
|
-
type: 'mdxJsxFlowElement',
|
|
224
|
-
name: 'img',
|
|
225
|
-
children: [],
|
|
226
|
-
attributes: [
|
|
227
|
-
alt && {
|
|
228
|
-
type: 'mdxJsxAttribute',
|
|
229
|
-
name: 'alt',
|
|
230
|
-
value: alt
|
|
231
|
-
},
|
|
232
|
-
getMdxSrcAttribute(tempVariableName)
|
|
233
|
-
].filter(Boolean)
|
|
234
|
-
});
|
|
235
|
-
images.push(getASTNodeImport(tempVariableName, imagePath));
|
|
236
|
-
});
|
|
237
|
-
visit(tree, (node)=>{
|
|
238
|
-
if ('mdxJsxFlowElement' !== node.type && 'mdxJsxTextElement' !== node.type || 'img' !== node.name) return;
|
|
239
|
-
const srcAttr = getNodeAttribute(node, 'src', true);
|
|
240
|
-
if ('string' != typeof srcAttr?.value) return;
|
|
241
|
-
const imagePath = normalizeImageUrl(srcAttr.value);
|
|
242
|
-
if (!imagePath) return;
|
|
243
|
-
const tempVariableName = `image${images.length}`;
|
|
244
|
-
Object.assign(srcAttr, getMdxSrcAttribute(tempVariableName));
|
|
245
|
-
images.push(getASTNodeImport(tempVariableName, imagePath));
|
|
246
|
-
});
|
|
247
|
-
tree.children.unshift(...images);
|
|
248
|
-
};
|
|
249
|
-
const parseToc = (tree)=>{
|
|
250
|
-
let title = '';
|
|
251
|
-
const toc = [];
|
|
252
|
-
const slugger = new github_slugger();
|
|
253
|
-
visitChildren((node)=>{
|
|
254
|
-
if ('heading' !== node.type || !node.depth || !node.children) return;
|
|
255
|
-
if (node.depth >= 1 && node.depth < 5) {
|
|
256
|
-
let customId = '';
|
|
257
|
-
const text = node.children.map((child)=>{
|
|
258
|
-
if ('link' === child.type) return child.children?.map((item)=>item.value).join('');
|
|
259
|
-
if ('strong' === child.type) return `**${child.children?.map((item)=>item.value).join('')}**`;
|
|
260
|
-
if ('emphasis' === child.type) return `*${child.children?.map((item)=>item.value).join('')}*`;
|
|
261
|
-
if ('delete' === child.type) return `~~${child.children?.map((item)=>item.value).join('')}~~`;
|
|
262
|
-
if ('text' === child.type) {
|
|
263
|
-
const [textPart, idPart] = extractTextAndId(child.value);
|
|
264
|
-
customId = idPart;
|
|
265
|
-
return textPart;
|
|
266
|
-
}
|
|
267
|
-
if ('inlineCode' === child.type) return `\`${child.value}\``;
|
|
268
|
-
return '';
|
|
269
|
-
}).join('');
|
|
270
|
-
if (1 === node.depth) {
|
|
271
|
-
if (!title) title = text;
|
|
272
|
-
} else {
|
|
273
|
-
const id = customId ? customId : slugger.slug(text.trim());
|
|
274
|
-
const { depth } = node;
|
|
275
|
-
toc.push({
|
|
276
|
-
id,
|
|
277
|
-
text,
|
|
278
|
-
depth
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
})(tree);
|
|
283
|
-
return {
|
|
284
|
-
title,
|
|
285
|
-
toc
|
|
286
|
-
};
|
|
287
|
-
};
|
|
288
|
-
const remarkPluginToc = function() {
|
|
289
|
-
const data = this.data();
|
|
290
|
-
return (tree)=>{
|
|
291
|
-
const { toc, title } = parseToc(tree);
|
|
292
|
-
data.pageMeta.toc = toc;
|
|
293
|
-
if (title) data.pageMeta.title = title;
|
|
294
|
-
};
|
|
295
|
-
};
|
|
296
|
-
const rehypeCodeMeta = ()=>(tree)=>{
|
|
297
|
-
visit(tree, 'element', (node)=>{
|
|
298
|
-
if ('pre' === node.tagName && node.children[0]?.type === 'element' && 'code' === node.children[0].tagName) {
|
|
299
|
-
const codeNode = node.children[0];
|
|
300
|
-
codeNode.properties.metastring = codeNode.data?.meta;
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
};
|
|
304
91
|
const SHIKI_TRANSFORMER_LINE_NUMBER = 'shiki-transformer:line-number';
|
|
305
92
|
function transformerLineNumber(options = {}) {
|
|
306
93
|
const { classActiveLine = 'line-number', classActivePre = 'has-line-number' } = options;
|
|
@@ -364,6 +151,43 @@ function createRehypeShikiOptions(showLineNumbers, options) {
|
|
|
364
151
|
transformers: newTransformers
|
|
365
152
|
};
|
|
366
153
|
}
|
|
154
|
+
const getASTNodeImport = (name, from)=>({
|
|
155
|
+
type: 'mdxjsEsm',
|
|
156
|
+
value: `import ${name} from ${JSON.stringify(from)}`,
|
|
157
|
+
data: {
|
|
158
|
+
estree: {
|
|
159
|
+
type: 'Program',
|
|
160
|
+
sourceType: 'module',
|
|
161
|
+
body: [
|
|
162
|
+
{
|
|
163
|
+
type: 'ImportDeclaration',
|
|
164
|
+
specifiers: [
|
|
165
|
+
{
|
|
166
|
+
type: 'ImportDefaultSpecifier',
|
|
167
|
+
local: {
|
|
168
|
+
type: 'Identifier',
|
|
169
|
+
name
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
],
|
|
173
|
+
source: {
|
|
174
|
+
type: 'Literal',
|
|
175
|
+
value: from,
|
|
176
|
+
raw: `${JSON.stringify(from)}`
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
]
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
const remarkBuiltin = ({ globalComponents })=>(tree)=>{
|
|
184
|
+
const demos = globalComponents.map((componentPath)=>{
|
|
185
|
+
const filename = node_path.parse(componentPath).name;
|
|
186
|
+
const componentName = filename[0].toUpperCase() + filename.slice(1);
|
|
187
|
+
return getASTNodeImport(componentName, componentPath);
|
|
188
|
+
});
|
|
189
|
+
tree.children.unshift(...demos);
|
|
190
|
+
};
|
|
367
191
|
const DIRECTIVE_TYPES = [
|
|
368
192
|
'tip',
|
|
369
193
|
'note',
|
|
@@ -573,7 +397,7 @@ function parseFileFromMeta(meta) {
|
|
|
573
397
|
}
|
|
574
398
|
return '';
|
|
575
399
|
}
|
|
576
|
-
const remarkFileCodeBlock = ({ filepath })=>async (tree)=>{
|
|
400
|
+
const remarkFileCodeBlock = ({ filepath, addDependency })=>async (tree)=>{
|
|
577
401
|
const promiseList = [];
|
|
578
402
|
visit(tree, 'code', (node)=>{
|
|
579
403
|
const { meta, value, lang } = node;
|
|
@@ -597,6 +421,7 @@ this usage is not allowed, please use below:
|
|
|
597
421
|
throw new Error(`${ERROR_PREFIX} ${originalMetaForErrorInfo} The content of file code block should be empty.`);
|
|
598
422
|
}
|
|
599
423
|
const promise = promises_readFile(resolvedFilePath, 'utf-8').then((fileContent)=>{
|
|
424
|
+
addDependency?.(resolvedFilePath);
|
|
600
425
|
node.value = fileContent;
|
|
601
426
|
}).catch((e)=>{
|
|
602
427
|
const message = `${ERROR_PREFIX} ${originalMetaForErrorInfo} introduces another file in "${resolvedFilePath}", but the file does not exist.`;
|
|
@@ -617,8 +442,209 @@ Please use below:
|
|
|
617
442
|
});
|
|
618
443
|
await Promise.all(promiseList);
|
|
619
444
|
};
|
|
445
|
+
const normalizeImageUrl = (imageUrl)=>{
|
|
446
|
+
if (isExternalUrl(imageUrl) || imageUrl.startsWith('/')) return '';
|
|
447
|
+
return imageUrl;
|
|
448
|
+
};
|
|
449
|
+
const remarkImage = ()=>(tree, _file)=>{
|
|
450
|
+
const images = [];
|
|
451
|
+
const getMdxSrcAttribute = (tempVar)=>({
|
|
452
|
+
type: 'mdxJsxAttribute',
|
|
453
|
+
name: 'src',
|
|
454
|
+
value: {
|
|
455
|
+
type: 'mdxJsxAttributeValueExpression',
|
|
456
|
+
value: tempVar,
|
|
457
|
+
data: {
|
|
458
|
+
estree: {
|
|
459
|
+
type: 'Program',
|
|
460
|
+
sourceType: 'module',
|
|
461
|
+
body: [
|
|
462
|
+
{
|
|
463
|
+
type: 'ExpressionStatement',
|
|
464
|
+
expression: {
|
|
465
|
+
type: 'Identifier',
|
|
466
|
+
name: tempVar
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
]
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
visit(tree, 'image', (node)=>{
|
|
475
|
+
const { alt, url } = node;
|
|
476
|
+
if (!url) return;
|
|
477
|
+
const imagePath = normalizeImageUrl(url);
|
|
478
|
+
if (!imagePath) return;
|
|
479
|
+
const tempVariableName = `image${images.length}`;
|
|
480
|
+
Object.assign(node, {
|
|
481
|
+
type: 'mdxJsxFlowElement',
|
|
482
|
+
name: 'img',
|
|
483
|
+
children: [],
|
|
484
|
+
attributes: [
|
|
485
|
+
alt && {
|
|
486
|
+
type: 'mdxJsxAttribute',
|
|
487
|
+
name: 'alt',
|
|
488
|
+
value: alt
|
|
489
|
+
},
|
|
490
|
+
getMdxSrcAttribute(tempVariableName)
|
|
491
|
+
].filter(Boolean)
|
|
492
|
+
});
|
|
493
|
+
images.push(getASTNodeImport(tempVariableName, imagePath));
|
|
494
|
+
});
|
|
495
|
+
visit(tree, (node)=>{
|
|
496
|
+
if ('mdxJsxFlowElement' !== node.type && 'mdxJsxTextElement' !== node.type || 'img' !== node.name) return;
|
|
497
|
+
const srcAttr = getNodeAttribute(node, 'src', true);
|
|
498
|
+
if ('string' != typeof srcAttr?.value) return;
|
|
499
|
+
const imagePath = normalizeImageUrl(srcAttr.value);
|
|
500
|
+
if (!imagePath) return;
|
|
501
|
+
const tempVariableName = `image${images.length}`;
|
|
502
|
+
Object.assign(srcAttr, getMdxSrcAttribute(tempVariableName));
|
|
503
|
+
images.push(getASTNodeImport(tempVariableName, imagePath));
|
|
504
|
+
});
|
|
505
|
+
tree.children.unshift(...images);
|
|
506
|
+
};
|
|
507
|
+
let isLogged = false;
|
|
508
|
+
function hintRelativeMarkdownLink() {
|
|
509
|
+
if (isLogged) return;
|
|
510
|
+
isLogged = true;
|
|
511
|
+
logger_logger.info('[Rspress v2] Markdown links without "./" prefix are now relative links, e.g: [](guide/getting-started) is equal to [](./guide/getting-started). You can rewrite it to [](/guide/getting-started) to always use absolute links, or use [](./getting-started.md)');
|
|
512
|
+
}
|
|
513
|
+
function link_checkDeadLinks(checkDeadLinks, internalLinks, filePath, routeService) {
|
|
514
|
+
const errorInfos = [];
|
|
515
|
+
const excludes = 'object' == typeof checkDeadLinks ? checkDeadLinks.excludes : void 0;
|
|
516
|
+
const excludesUrl = Array.isArray(excludes) ? new Set(excludes) : void 0;
|
|
517
|
+
const excludesFn = 'function' == typeof excludes ? excludes : void 0;
|
|
518
|
+
let possibleBreakingChange = false;
|
|
519
|
+
[
|
|
520
|
+
...internalLinks.entries()
|
|
521
|
+
].forEach(([nodeUrl, link])=>{
|
|
522
|
+
if (excludesUrl?.has(nodeUrl)) return;
|
|
523
|
+
if (excludesFn?.(nodeUrl)) return;
|
|
524
|
+
const cleanLinkPath = linkToRoutePath(link);
|
|
525
|
+
if (!cleanLinkPath) return;
|
|
526
|
+
if (!nodeUrl.startsWith('/') && /^\w/.test(nodeUrl)) possibleBreakingChange = true;
|
|
527
|
+
if (!routeService.isExistRoute(removeTrailingSlash(cleanLinkPath)) && !routeService.isExistRoute(addTrailingSlash(cleanLinkPath))) errorInfos.push([
|
|
528
|
+
nodeUrl,
|
|
529
|
+
link
|
|
530
|
+
]);
|
|
531
|
+
});
|
|
532
|
+
if (errorInfos.length > 0) {
|
|
533
|
+
if (possibleBreakingChange) hintRelativeMarkdownLink();
|
|
534
|
+
logger_logger.error(`Dead links found in ${picocolors_0.cyan(filePath)}:
|
|
535
|
+
${errorInfos.map(([nodeUrl, link])=>` ${picocolors_0.green(`"[..](${nodeUrl})"`)} ${picocolors_0.gray(link)}`).join('\n')}`);
|
|
536
|
+
if (isProduction()) throw new Error('Dead link found');
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
function looseMarkdownLink(url, routeService, filePath) {
|
|
540
|
+
if (!routeService.isInDocsDir(filePath)) return url;
|
|
541
|
+
const [versionPart, langPart, pureRoute] = routeService.splitRoutePathParts(url);
|
|
542
|
+
const relativePath = routeService.absolutePathToRelativePath(filePath);
|
|
543
|
+
const [versionPartCurrFile, langPartCurrFile] = routeService.getRoutePathParts(relativePath);
|
|
544
|
+
if ('' === versionPart) {
|
|
545
|
+
if ('' === langPart) return addLeadingSlash(`${[
|
|
546
|
+
versionPartCurrFile,
|
|
547
|
+
langPartCurrFile
|
|
548
|
+
].filter(Boolean).join('/')}${addLeadingSlash(pureRoute.replace(/^\/*/, ''))}`);
|
|
549
|
+
return addLeadingSlash(`${[
|
|
550
|
+
versionPartCurrFile,
|
|
551
|
+
langPart
|
|
552
|
+
].filter(Boolean).join('/')}${addLeadingSlash(pureRoute.replace(/^\/*/, ''))}`);
|
|
553
|
+
}
|
|
554
|
+
return url;
|
|
555
|
+
}
|
|
556
|
+
function normalizeLink(nodeUrl, routeService, filePath, cleanUrls, autoPrefix, internalLinks, __base) {
|
|
557
|
+
if (!nodeUrl) return '';
|
|
558
|
+
if (nodeUrl.startsWith('#')) return nodeUrl;
|
|
559
|
+
let { url, hash } = parseUrl(nodeUrl);
|
|
560
|
+
if (isExternalUrl(url)) return url + (hash ? `#${hash}` : '');
|
|
561
|
+
if (!routeService) return nodeUrl;
|
|
562
|
+
if (url.startsWith('/')) {
|
|
563
|
+
if (autoPrefix) url = looseMarkdownLink(url, routeService, filePath);
|
|
564
|
+
const { routePath } = routeService.normalizeRoutePath(url);
|
|
565
|
+
url = routePath;
|
|
566
|
+
} else {
|
|
567
|
+
const anotherFileAbsolutePath = node_path.join(node_path.dirname(filePath), url);
|
|
568
|
+
url = routeService.absolutePathToRoutePath(anotherFileAbsolutePath);
|
|
569
|
+
}
|
|
570
|
+
if ('boolean' == typeof cleanUrls) url = normalizeHref(url, cleanUrls);
|
|
571
|
+
else {
|
|
572
|
+
url = normalizeHref(url, false);
|
|
573
|
+
url = url.replace(/\.html$/, cleanUrls);
|
|
574
|
+
}
|
|
575
|
+
internalLinks.set(nodeUrl, url);
|
|
576
|
+
if (hash) url += `#${hash}`;
|
|
577
|
+
if (__base) url = withBase(url, __base);
|
|
578
|
+
return url;
|
|
579
|
+
}
|
|
580
|
+
function linkToRoutePath(routePath) {
|
|
581
|
+
return decodeURIComponent(routePath.split('#')[0]).replace(/\.html$/, '').replace(/\/index$/, '/');
|
|
582
|
+
}
|
|
583
|
+
const remarkLink = ({ cleanUrls, routeService, remarkLinkOptions, __base })=>(tree, file)=>{
|
|
584
|
+
const { checkDeadLinks: shouldCheckDeadLinks = true, autoPrefix = true } = remarkLinkOptions ?? {};
|
|
585
|
+
const internalLinks = new Map();
|
|
586
|
+
visit(tree, 'link', (node)=>{
|
|
587
|
+
const { url: nodeUrl } = node;
|
|
588
|
+
const link = normalizeLink(nodeUrl, routeService, file.path, cleanUrls, autoPrefix, internalLinks, __base);
|
|
589
|
+
node.url = link;
|
|
590
|
+
});
|
|
591
|
+
visit(tree, 'definition', (node)=>{
|
|
592
|
+
const { url: nodeUrl } = node;
|
|
593
|
+
const link = normalizeLink(nodeUrl, routeService, file.path, cleanUrls, autoPrefix, internalLinks, __base);
|
|
594
|
+
node.url = link;
|
|
595
|
+
});
|
|
596
|
+
if (shouldCheckDeadLinks && routeService) link_checkDeadLinks(shouldCheckDeadLinks, internalLinks, file.path, routeService);
|
|
597
|
+
};
|
|
598
|
+
const parseToc = (tree)=>{
|
|
599
|
+
let title = '';
|
|
600
|
+
const toc = [];
|
|
601
|
+
const slugger = new github_slugger();
|
|
602
|
+
visitChildren((node)=>{
|
|
603
|
+
if ('heading' !== node.type || !node.depth || !node.children) return;
|
|
604
|
+
if (node.depth >= 1 && node.depth < 5) {
|
|
605
|
+
let customId = '';
|
|
606
|
+
const text = node.children.map((child)=>{
|
|
607
|
+
if ('link' === child.type) return child.children?.map((item)=>item.value).join('');
|
|
608
|
+
if ('strong' === child.type) return `**${child.children?.map((item)=>item.value).join('')}**`;
|
|
609
|
+
if ('emphasis' === child.type) return `*${child.children?.map((item)=>item.value).join('')}*`;
|
|
610
|
+
if ('delete' === child.type) return `~~${child.children?.map((item)=>item.value).join('')}~~`;
|
|
611
|
+
if ('text' === child.type) {
|
|
612
|
+
const [textPart, idPart] = extractTextAndId(child.value);
|
|
613
|
+
customId = idPart;
|
|
614
|
+
return textPart;
|
|
615
|
+
}
|
|
616
|
+
if ('inlineCode' === child.type) return `\`${child.value}\``;
|
|
617
|
+
return '';
|
|
618
|
+
}).join('');
|
|
619
|
+
if (1 === node.depth) {
|
|
620
|
+
if (!title) title = text;
|
|
621
|
+
} else {
|
|
622
|
+
const id = customId ? customId : slugger.slug(text.trim());
|
|
623
|
+
const { depth } = node;
|
|
624
|
+
toc.push({
|
|
625
|
+
id,
|
|
626
|
+
text,
|
|
627
|
+
depth
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
})(tree);
|
|
632
|
+
return {
|
|
633
|
+
title,
|
|
634
|
+
toc
|
|
635
|
+
};
|
|
636
|
+
};
|
|
637
|
+
const remarkToc = function() {
|
|
638
|
+
const data = this.data();
|
|
639
|
+
return (tree)=>{
|
|
640
|
+
const { toc, title } = parseToc(tree);
|
|
641
|
+
data.pageMeta.toc = toc;
|
|
642
|
+
if (title) data.pageMeta.title = title;
|
|
643
|
+
};
|
|
644
|
+
};
|
|
620
645
|
async function createMDXOptions(options) {
|
|
621
|
-
const { docDirectory, config,
|
|
646
|
+
const { docDirectory, config, routeService, filepath, pluginDriver, addDependency } = options;
|
|
647
|
+
const remarkLinkOptions = config?.markdown?.link;
|
|
622
648
|
const format = node_path.extname(filepath).slice(1);
|
|
623
649
|
const cleanUrls = config?.route?.cleanUrls ?? false;
|
|
624
650
|
const { remarkPlugins: remarkPluginsFromConfig = [], rehypePlugins: rehypePluginsFromConfig = [], globalComponents: globalComponentsFromConfig = [], showLineNumbers = false, shiki } = config?.markdown || {};
|
|
@@ -634,23 +660,25 @@ async function createMDXOptions(options) {
|
|
|
634
660
|
format,
|
|
635
661
|
remarkPlugins: [
|
|
636
662
|
remark_gfm,
|
|
637
|
-
|
|
663
|
+
remarkToc,
|
|
638
664
|
remarkContainerSyntax,
|
|
639
665
|
[
|
|
640
666
|
remarkFileCodeBlock,
|
|
641
667
|
{
|
|
642
|
-
filepath
|
|
668
|
+
filepath,
|
|
669
|
+
addDependency
|
|
643
670
|
}
|
|
644
671
|
],
|
|
645
672
|
[
|
|
646
|
-
|
|
673
|
+
remarkLink,
|
|
647
674
|
{
|
|
648
675
|
cleanUrls,
|
|
649
676
|
root: docDirectory,
|
|
650
677
|
routeService,
|
|
651
|
-
|
|
678
|
+
remarkLinkOptions
|
|
652
679
|
}
|
|
653
680
|
],
|
|
681
|
+
remarkImage,
|
|
654
682
|
globalComponents.length && [
|
|
655
683
|
remarkBuiltin,
|
|
656
684
|
{
|
|
@@ -688,14 +716,14 @@ async function createMDXOptions(options) {
|
|
|
688
716
|
};
|
|
689
717
|
}
|
|
690
718
|
async function compile(options) {
|
|
691
|
-
const { source, filepath, docDirectory,
|
|
719
|
+
const { source, filepath, docDirectory, config, routeService, pluginDriver, addDependency } = options;
|
|
692
720
|
const mdxOptions = await createMDXOptions({
|
|
693
|
-
checkDeadLinks,
|
|
694
721
|
config,
|
|
695
722
|
docDirectory,
|
|
696
723
|
filepath,
|
|
697
724
|
pluginDriver,
|
|
698
|
-
routeService
|
|
725
|
+
routeService,
|
|
726
|
+
addDependency
|
|
699
727
|
});
|
|
700
728
|
const { frontmatter, emptyLinesSource } = loadFrontMatter(source, filepath, docDirectory, true);
|
|
701
729
|
const { replaceRules } = config ?? {};
|
|
@@ -740,10 +768,14 @@ MDXContent.__RSPRESS_PAGE_META["${encodeURIComponent(normalizePath(node_path.rel
|
|
|
740
768
|
}
|
|
741
769
|
const cache = new Map();
|
|
742
770
|
async function compileWithCrossCompilerCache(options) {
|
|
743
|
-
const
|
|
744
|
-
|
|
771
|
+
const filepath = options.filepath;
|
|
772
|
+
const task = cache.get(filepath);
|
|
773
|
+
if (task) {
|
|
774
|
+
cache.delete(filepath);
|
|
775
|
+
return task;
|
|
776
|
+
}
|
|
745
777
|
const promise = compile(options);
|
|
746
|
-
cache.set(
|
|
778
|
+
cache.set(filepath, promise);
|
|
747
779
|
return promise;
|
|
748
780
|
}
|
|
749
781
|
export { compile, compileWithCrossCompilerCache };
|
package/dist/renderPageWorker.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { workerData } from "node:worker_threads";
|
|
2
2
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
3
|
-
import { createHead, transformHtmlTemplate } from "@unhead/react/server";
|
|
4
3
|
import { logger as logger_logger } from "@rspress/shared/logger";
|
|
4
|
+
import { createHead, transformHtmlTemplate } from "@unhead/react/server";
|
|
5
5
|
import picocolors_0 from "picocolors";
|
|
6
6
|
import "node:fs/promises";
|
|
7
7
|
import node_path from "node:path";
|
|
@@ -102,7 +102,7 @@ function hintSSGFailed() {
|
|
|
102
102
|
1. Fix code to make it SSR-compatible.
|
|
103
103
|
2. Set \`ssg: false\` if the code in node_modules may be difficult to fix, but the SSG feature will be lost.`);
|
|
104
104
|
}
|
|
105
|
-
var package_namespaceObject = JSON.parse('{"i8":"2.0.0-beta.
|
|
105
|
+
var package_namespaceObject = JSON.parse('{"i8":"2.0.0-beta.24"}');
|
|
106
106
|
const RSPRESS_VERSION = package_namespaceObject.i8;
|
|
107
107
|
`{
|
|
108
108
|
const saved = localStorage.getItem('${APPEARANCE_KEY}')
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { PassThrough } from "node:stream";
|
|
3
|
+
import { text as consumers_text } from "node:stream/consumers";
|
|
2
4
|
import { DataContext, ThemeContext, pathnameToRouteService, withBase } from "@rspress/runtime";
|
|
3
5
|
import { StaticRouter } from "@rspress/runtime/server";
|
|
4
6
|
import { UnheadProvider } from "@unhead/react/server";
|
|
5
7
|
import { renderToPipeableStream } from "react-dom/server";
|
|
6
|
-
import { PassThrough } from "node:stream";
|
|
7
|
-
import { text as consumers_text } from "node:stream/consumers";
|
|
8
8
|
import { base } from "virtual-runtime-config";
|
|
9
9
|
import { App } from "./App.js";
|
|
10
10
|
import { initPageData } from "./initPageData.js";
|
package/dist/runtime.d.ts
CHANGED
package/dist/runtime.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { isExternalUrl } from "@rspress/shared";
|
|
1
|
+
import { isExternalUrl, normalizeHref } from "@rspress/shared";
|
|
2
2
|
export * from "@rspress/runtime";
|
|
3
|
-
export { isExternalUrl };
|
|
3
|
+
export { isExternalUrl, normalizeHref };
|