@rspress/core 2.0.0-beta.10 → 2.0.0-beta.12
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/index.d.ts +2 -2
- package/dist/index.js +303 -200
- package/dist/loader.d.ts +6 -20
- package/dist/loader.js +28 -397
- package/dist/processor.d.ts +73 -0
- package/dist/processor.js +424 -0
- package/dist/runtime/App.js +16 -16
- package/dist/runtime/ClientApp.js +16 -16
- package/dist/runtime/csrClientEntry.js +4 -4
- package/dist/runtime/initPageData.js +17 -17
- package/dist/runtime/ssrClientEntry.js +15 -8
- package/dist/runtime/ssrServerEntry.js +36 -16
- package/package.json +16 -15
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import __rslib_shim_module__ from 'module';
|
|
2
|
+
/*#__PURE__*/ import.meta.url;
|
|
3
|
+
import external_node_path_default from "node:path";
|
|
4
|
+
import { createProcessor, nodeTypes } from "@mdx-js/mdx";
|
|
5
|
+
import { logger } from "@rspress/shared/logger";
|
|
6
|
+
import { extractTextAndId, getNodeAttribute, loadFrontMatter } from "@rspress/shared/node-utils";
|
|
7
|
+
import external_node_os_default from "node:os";
|
|
8
|
+
import external_rehype_external_links_default from "rehype-external-links";
|
|
9
|
+
import external_rehype_raw_default from "rehype-raw";
|
|
10
|
+
import external_remark_gfm_default from "remark-gfm";
|
|
11
|
+
import external_github_slugger_default from "github-slugger";
|
|
12
|
+
import { headingRank } from "hast-util-heading-rank";
|
|
13
|
+
import { visit } from "unist-util-visit";
|
|
14
|
+
import { addLeadingSlash, cleanUrl, isExternalUrl, isProduction, normalizeHref, parseUrl, slash as shared_slash } from "@rspress/shared";
|
|
15
|
+
import { DEFAULT_PAGE_EXTENSIONS } from "@rspress/shared/constants";
|
|
16
|
+
import { visitChildren } from "unist-util-visit-children";
|
|
17
|
+
function applyReplaceRules(code = '', replaceRules = []) {
|
|
18
|
+
let result = code;
|
|
19
|
+
for (const rule of replaceRules)result = result.replace(rule.search, rule.replace);
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
function escapeMarkdownHeadingIds(content) {
|
|
23
|
+
const markdownHeadingRegexp = /(?:^|\n)#{1,6}(?!#).*/g;
|
|
24
|
+
return content.replace(markdownHeadingRegexp, (substring)=>substring.replace('{#', '\\{#').replace('\\\\{#', '\\{#'));
|
|
25
|
+
}
|
|
26
|
+
const isWindows = 'win32' === external_node_os_default.platform();
|
|
27
|
+
function slash(p) {
|
|
28
|
+
return p.replace(/\\/g, '/');
|
|
29
|
+
}
|
|
30
|
+
function normalizePath(id) {
|
|
31
|
+
return external_node_path_default.posix.normalize(isWindows ? slash(id) : id);
|
|
32
|
+
}
|
|
33
|
+
const rehypeHeaderAnchor = ()=>{
|
|
34
|
+
const slugger = new external_github_slugger_default();
|
|
35
|
+
return (tree)=>{
|
|
36
|
+
visit(tree, 'element', (node)=>{
|
|
37
|
+
if (!headingRank(node)) return;
|
|
38
|
+
if (!node.properties?.id) {
|
|
39
|
+
const [text, customId] = collectHeaderText(node);
|
|
40
|
+
node.properties ??= {};
|
|
41
|
+
node.properties.id = customId || slugger.slug(text);
|
|
42
|
+
}
|
|
43
|
+
node.children.unshift(create(node));
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const collectHeaderText = (node)=>{
|
|
48
|
+
let text = '';
|
|
49
|
+
let id = '';
|
|
50
|
+
node.children.forEach((child)=>{
|
|
51
|
+
if ('text' === child.type) {
|
|
52
|
+
const [textPart, idPart] = extractTextAndId(child.value);
|
|
53
|
+
child.value = textPart;
|
|
54
|
+
text += textPart;
|
|
55
|
+
id = idPart;
|
|
56
|
+
} else if ('element' === child.type) child.children.forEach((c)=>{
|
|
57
|
+
if ('text' === c.type) text += c.value;
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
return [
|
|
61
|
+
text,
|
|
62
|
+
id
|
|
63
|
+
];
|
|
64
|
+
};
|
|
65
|
+
function create(node) {
|
|
66
|
+
return {
|
|
67
|
+
type: 'element',
|
|
68
|
+
tagName: 'a',
|
|
69
|
+
properties: {
|
|
70
|
+
class: 'header-anchor',
|
|
71
|
+
ariaHidden: 'true',
|
|
72
|
+
href: `#${node.properties.id}`
|
|
73
|
+
},
|
|
74
|
+
children: [
|
|
75
|
+
{
|
|
76
|
+
type: 'text',
|
|
77
|
+
value: '#'
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
const getASTNodeImport = (name, from)=>({
|
|
83
|
+
type: 'mdxjsEsm',
|
|
84
|
+
value: `import ${name} from ${JSON.stringify(from)}`,
|
|
85
|
+
data: {
|
|
86
|
+
estree: {
|
|
87
|
+
type: 'Program',
|
|
88
|
+
sourceType: 'module',
|
|
89
|
+
body: [
|
|
90
|
+
{
|
|
91
|
+
type: 'ImportDeclaration',
|
|
92
|
+
specifiers: [
|
|
93
|
+
{
|
|
94
|
+
type: 'ImportDefaultSpecifier',
|
|
95
|
+
local: {
|
|
96
|
+
type: 'Identifier',
|
|
97
|
+
name
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
],
|
|
101
|
+
source: {
|
|
102
|
+
type: 'Literal',
|
|
103
|
+
value: from,
|
|
104
|
+
raw: `${JSON.stringify(from)}`
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
const remarkBuiltin = ({ globalComponents })=>(tree)=>{
|
|
112
|
+
const demos = globalComponents.map((componentPath)=>{
|
|
113
|
+
const filename = external_node_path_default.parse(componentPath).name;
|
|
114
|
+
const componentName = filename[0].toUpperCase() + filename.slice(1);
|
|
115
|
+
return getASTNodeImport(componentName, componentPath);
|
|
116
|
+
});
|
|
117
|
+
tree.children.unshift(...demos);
|
|
118
|
+
};
|
|
119
|
+
const IGNORE_REGEXP = /^(https?|mailto|tel|#)/;
|
|
120
|
+
function checkLinks(links, filepath, root, routeService) {
|
|
121
|
+
const errorInfos = [];
|
|
122
|
+
links.filter((link)=>!IGNORE_REGEXP.test(link)).map((link)=>normalizePath(link)).forEach((link)=>{
|
|
123
|
+
const relativePath = external_node_path_default.relative(root, filepath);
|
|
124
|
+
if (!routeService.isExistRoute(cleanUrl(link))) errorInfos.push(`Internal link to "${link}" is dead, check it in "${relativePath}"`);
|
|
125
|
+
});
|
|
126
|
+
if (errorInfos.length > 0) {
|
|
127
|
+
errorInfos?.forEach((err)=>{
|
|
128
|
+
logger.error(err);
|
|
129
|
+
});
|
|
130
|
+
if (isProduction()) throw new Error('Dead link found');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const remarkCheckDeadLinks = (checkLink)=>{
|
|
134
|
+
const { root, routeService } = checkLink;
|
|
135
|
+
return (tree, vfile)=>{
|
|
136
|
+
const internalLinks = new Set();
|
|
137
|
+
visit(tree, 'link', ({ url })=>{
|
|
138
|
+
if (!url) return;
|
|
139
|
+
if (internalLinks.has(url)) return;
|
|
140
|
+
if (!url.startsWith('http://') && !url.startsWith('https://')) {
|
|
141
|
+
const { routePath: normalizeUrl } = routeService.normalizeRoutePath(url.split(external_node_path_default.sep).join('/')?.split('#')[0]);
|
|
142
|
+
internalLinks.add(normalizeUrl);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
checkLinks(Array.from(internalLinks), vfile.path, root, routeService);
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
function normalizeLink(nodeUrl, routeService, relativePath, cleanUrls) {
|
|
149
|
+
if (!nodeUrl) return '';
|
|
150
|
+
if (nodeUrl.startsWith('#')) return `#${nodeUrl.slice(1)}`;
|
|
151
|
+
let { url, hash } = parseUrl(nodeUrl);
|
|
152
|
+
if (isExternalUrl(url)) return url + (hash ? `#${hash}` : '');
|
|
153
|
+
const extname = external_node_path_default.extname(url);
|
|
154
|
+
if ((routeService?.extensions ?? DEFAULT_PAGE_EXTENSIONS).includes(extname)) url = url.replace(new RegExp(`\\${extname}$`), '');
|
|
155
|
+
if (url.startsWith('.')) url = external_node_path_default.posix.join(shared_slash(external_node_path_default.dirname(relativePath)), url);
|
|
156
|
+
else if (routeService) {
|
|
157
|
+
const [pathVersion, pathLang] = routeService.getRoutePathParts(shared_slash(relativePath));
|
|
158
|
+
const [urlVersion, urlLang, urlPath] = routeService.getRoutePathParts(url);
|
|
159
|
+
url = addLeadingSlash(urlPath);
|
|
160
|
+
if (pathLang && urlLang !== pathLang) url = `/${pathLang}${url}`;
|
|
161
|
+
if (pathVersion && urlVersion !== pathVersion) url = `/${pathVersion}${url}`;
|
|
162
|
+
}
|
|
163
|
+
if ('boolean' == typeof cleanUrls) url = normalizeHref(url, cleanUrls);
|
|
164
|
+
else {
|
|
165
|
+
url = normalizeHref(url, false);
|
|
166
|
+
url = url.replace(/\.html$/, cleanUrls);
|
|
167
|
+
}
|
|
168
|
+
if (hash) url += `#${hash}`;
|
|
169
|
+
return url;
|
|
170
|
+
}
|
|
171
|
+
const normalizeImageUrl = (imageUrl)=>{
|
|
172
|
+
if (isExternalUrl(imageUrl) || imageUrl.startsWith('/')) return '';
|
|
173
|
+
return imageUrl;
|
|
174
|
+
};
|
|
175
|
+
const remarkPluginNormalizeLink = ({ root, cleanUrls, routeService })=>(tree, file)=>{
|
|
176
|
+
const images = [];
|
|
177
|
+
visit(tree, 'link', (node)=>{
|
|
178
|
+
const { url: nodeUrl } = node;
|
|
179
|
+
const relativePath = external_node_path_default.relative(root, file.path);
|
|
180
|
+
node.url = normalizeLink(nodeUrl, routeService, relativePath, cleanUrls);
|
|
181
|
+
});
|
|
182
|
+
visit(tree, 'definition', (node)=>{
|
|
183
|
+
const { url: nodeUrl } = node;
|
|
184
|
+
const relativePath = external_node_path_default.relative(root, file.path);
|
|
185
|
+
node.url = normalizeLink(nodeUrl, routeService, relativePath, cleanUrls);
|
|
186
|
+
});
|
|
187
|
+
const getMdxSrcAttribute = (tempVar)=>({
|
|
188
|
+
type: 'mdxJsxAttribute',
|
|
189
|
+
name: 'src',
|
|
190
|
+
value: {
|
|
191
|
+
type: 'mdxJsxAttributeValueExpression',
|
|
192
|
+
value: tempVar,
|
|
193
|
+
data: {
|
|
194
|
+
estree: {
|
|
195
|
+
type: 'Program',
|
|
196
|
+
sourceType: 'module',
|
|
197
|
+
body: [
|
|
198
|
+
{
|
|
199
|
+
type: 'ExpressionStatement',
|
|
200
|
+
expression: {
|
|
201
|
+
type: 'Identifier',
|
|
202
|
+
name: tempVar
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
]
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
visit(tree, 'image', (node)=>{
|
|
211
|
+
const { alt, url } = node;
|
|
212
|
+
if (!url) return;
|
|
213
|
+
const imagePath = normalizeImageUrl(url);
|
|
214
|
+
if (!imagePath) return;
|
|
215
|
+
const tempVariableName = `image${images.length}`;
|
|
216
|
+
Object.assign(node, {
|
|
217
|
+
type: 'mdxJsxFlowElement',
|
|
218
|
+
name: 'img',
|
|
219
|
+
children: [],
|
|
220
|
+
attributes: [
|
|
221
|
+
alt && {
|
|
222
|
+
type: 'mdxJsxAttribute',
|
|
223
|
+
name: 'alt',
|
|
224
|
+
value: alt
|
|
225
|
+
},
|
|
226
|
+
getMdxSrcAttribute(tempVariableName)
|
|
227
|
+
].filter(Boolean)
|
|
228
|
+
});
|
|
229
|
+
images.push(getASTNodeImport(tempVariableName, imagePath));
|
|
230
|
+
});
|
|
231
|
+
visit(tree, (node)=>{
|
|
232
|
+
if ('mdxJsxFlowElement' !== node.type && 'mdxJsxTextElement' !== node.type || 'img' !== node.name) return;
|
|
233
|
+
const srcAttr = getNodeAttribute(node, 'src', true);
|
|
234
|
+
if ('string' != typeof srcAttr?.value) return;
|
|
235
|
+
const imagePath = normalizeImageUrl(srcAttr.value);
|
|
236
|
+
if (!imagePath) return;
|
|
237
|
+
const tempVariableName = `image${images.length}`;
|
|
238
|
+
Object.assign(srcAttr, getMdxSrcAttribute(tempVariableName));
|
|
239
|
+
images.push(getASTNodeImport(tempVariableName, imagePath));
|
|
240
|
+
});
|
|
241
|
+
tree.children.unshift(...images);
|
|
242
|
+
};
|
|
243
|
+
const parseToc = (tree)=>{
|
|
244
|
+
let title = '';
|
|
245
|
+
const toc = [];
|
|
246
|
+
const slugger = new external_github_slugger_default();
|
|
247
|
+
visitChildren((node)=>{
|
|
248
|
+
if ('heading' !== node.type || !node.depth || !node.children) return;
|
|
249
|
+
if (node.depth >= 1 && node.depth < 5) {
|
|
250
|
+
let customId = '';
|
|
251
|
+
const text = node.children.map((child)=>{
|
|
252
|
+
if ('link' === child.type) return child.children?.map((item)=>item.value).join('');
|
|
253
|
+
if ('strong' === child.type) return `**${child.children?.map((item)=>item.value).join('')}**`;
|
|
254
|
+
if ('emphasis' === child.type) return `*${child.children?.map((item)=>item.value).join('')}*`;
|
|
255
|
+
if ('delete' === child.type) return `~~${child.children?.map((item)=>item.value).join('')}~~`;
|
|
256
|
+
if ('text' === child.type) {
|
|
257
|
+
const [textPart, idPart] = extractTextAndId(child.value);
|
|
258
|
+
customId = idPart;
|
|
259
|
+
return textPart;
|
|
260
|
+
}
|
|
261
|
+
if ('inlineCode' === child.type) return `\`${child.value}\``;
|
|
262
|
+
return '';
|
|
263
|
+
}).join('');
|
|
264
|
+
if (1 === node.depth) {
|
|
265
|
+
if (!title) title = text;
|
|
266
|
+
} else {
|
|
267
|
+
const id = customId ? customId : slugger.slug(text);
|
|
268
|
+
const { depth } = node;
|
|
269
|
+
toc.push({
|
|
270
|
+
id,
|
|
271
|
+
text,
|
|
272
|
+
depth
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
})(tree);
|
|
277
|
+
return {
|
|
278
|
+
title,
|
|
279
|
+
toc
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
const remarkPluginToc = function() {
|
|
283
|
+
const data = this.data();
|
|
284
|
+
return (tree)=>{
|
|
285
|
+
const { toc, title } = parseToc(tree);
|
|
286
|
+
data.pageMeta.toc = toc;
|
|
287
|
+
if (title) data.pageMeta.title = title;
|
|
288
|
+
};
|
|
289
|
+
};
|
|
290
|
+
const rehypeCodeMeta = ()=>(tree)=>{
|
|
291
|
+
visit(tree, 'element', (node)=>{
|
|
292
|
+
if ('pre' === node.tagName && node.children[0]?.type === 'element' && 'code' === node.children[0].tagName) {
|
|
293
|
+
const codeNode = node.children[0];
|
|
294
|
+
codeNode.properties.metastring = codeNode.data?.meta;
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
};
|
|
298
|
+
async function createMDXOptions(options) {
|
|
299
|
+
const { docDirectory, config, checkDeadLinks, routeService, filepath, pluginDriver } = options;
|
|
300
|
+
const format = external_node_path_default.extname(filepath).slice(1);
|
|
301
|
+
const cleanUrls = config?.route?.cleanUrls ?? false;
|
|
302
|
+
const { remarkPlugins: remarkPluginsFromConfig = [], rehypePlugins: rehypePluginsFromConfig = [], globalComponents: globalComponentsFromConfig = [] } = config?.markdown || {};
|
|
303
|
+
const rspressPlugins = pluginDriver.getPlugins();
|
|
304
|
+
const remarkPluginsFromPlugins = rspressPlugins.flatMap((plugin)=>plugin.markdown?.remarkPlugins || []);
|
|
305
|
+
const rehypePluginsFromPlugins = rspressPlugins.flatMap((plugin)=>plugin.markdown?.rehypePlugins || []);
|
|
306
|
+
const globalComponents = [
|
|
307
|
+
...rspressPlugins.flatMap((plugin)=>plugin.markdown?.globalComponents || []),
|
|
308
|
+
...globalComponentsFromConfig
|
|
309
|
+
];
|
|
310
|
+
return {
|
|
311
|
+
providerImportSource: '@mdx-js/react',
|
|
312
|
+
format,
|
|
313
|
+
remarkPlugins: [
|
|
314
|
+
external_remark_gfm_default,
|
|
315
|
+
remarkPluginToc,
|
|
316
|
+
[
|
|
317
|
+
remarkPluginNormalizeLink,
|
|
318
|
+
{
|
|
319
|
+
cleanUrls,
|
|
320
|
+
root: docDirectory,
|
|
321
|
+
routeService
|
|
322
|
+
}
|
|
323
|
+
],
|
|
324
|
+
checkDeadLinks && [
|
|
325
|
+
remarkCheckDeadLinks,
|
|
326
|
+
{
|
|
327
|
+
root: docDirectory,
|
|
328
|
+
base: config?.base || '',
|
|
329
|
+
routeService
|
|
330
|
+
}
|
|
331
|
+
],
|
|
332
|
+
globalComponents.length && [
|
|
333
|
+
remarkBuiltin,
|
|
334
|
+
{
|
|
335
|
+
globalComponents
|
|
336
|
+
}
|
|
337
|
+
],
|
|
338
|
+
...remarkPluginsFromConfig,
|
|
339
|
+
...remarkPluginsFromPlugins
|
|
340
|
+
].filter(Boolean),
|
|
341
|
+
rehypePlugins: [
|
|
342
|
+
rehypeHeaderAnchor,
|
|
343
|
+
...'md' === format ? [
|
|
344
|
+
rehypeCodeMeta,
|
|
345
|
+
[
|
|
346
|
+
external_rehype_raw_default,
|
|
347
|
+
{
|
|
348
|
+
passThrough: nodeTypes
|
|
349
|
+
}
|
|
350
|
+
]
|
|
351
|
+
] : [],
|
|
352
|
+
[
|
|
353
|
+
external_rehype_external_links_default,
|
|
354
|
+
{
|
|
355
|
+
target: '_blank',
|
|
356
|
+
rel: 'noopener noreferrer'
|
|
357
|
+
}
|
|
358
|
+
],
|
|
359
|
+
...rehypePluginsFromConfig,
|
|
360
|
+
...rehypePluginsFromPlugins
|
|
361
|
+
]
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
async function compile(options) {
|
|
365
|
+
const { source, filepath, docDirectory, checkDeadLinks, config, routeService, pluginDriver } = options;
|
|
366
|
+
config.root;
|
|
367
|
+
const mdxOptions = await createMDXOptions({
|
|
368
|
+
checkDeadLinks,
|
|
369
|
+
config,
|
|
370
|
+
docDirectory,
|
|
371
|
+
filepath,
|
|
372
|
+
pluginDriver,
|
|
373
|
+
routeService
|
|
374
|
+
});
|
|
375
|
+
const { frontmatter, emptyLinesSource } = loadFrontMatter(source, filepath, docDirectory, true);
|
|
376
|
+
const replacedSource = applyReplaceRules(emptyLinesSource, config.replaceRules);
|
|
377
|
+
const preprocessedContent = escapeMarkdownHeadingIds(replacedSource);
|
|
378
|
+
try {
|
|
379
|
+
let pageMeta = {
|
|
380
|
+
title: '',
|
|
381
|
+
toc: [],
|
|
382
|
+
headingTitle: ''
|
|
383
|
+
};
|
|
384
|
+
const frontmatterTitle = extractTextAndId(frontmatter.title)[0];
|
|
385
|
+
const compiler = createProcessor(mdxOptions);
|
|
386
|
+
compiler.data('pageMeta', {
|
|
387
|
+
toc: [],
|
|
388
|
+
title: ''
|
|
389
|
+
});
|
|
390
|
+
const vFile = await compiler.process({
|
|
391
|
+
value: preprocessedContent,
|
|
392
|
+
path: filepath
|
|
393
|
+
});
|
|
394
|
+
const compileResult = String(vFile);
|
|
395
|
+
const compilationMeta = compiler.data('pageMeta');
|
|
396
|
+
const headingTitle = extractTextAndId(compilationMeta.title)[0];
|
|
397
|
+
pageMeta = {
|
|
398
|
+
...compilationMeta,
|
|
399
|
+
title: frontmatterTitle || headingTitle,
|
|
400
|
+
headingTitle,
|
|
401
|
+
frontmatter
|
|
402
|
+
};
|
|
403
|
+
const result = `const frontmatter = ${JSON.stringify(frontmatter)};
|
|
404
|
+
${compileResult}
|
|
405
|
+
MDXContent.__RSPRESS_PAGE_META = {};
|
|
406
|
+
|
|
407
|
+
MDXContent.__RSPRESS_PAGE_META["${encodeURIComponent(normalizePath(external_node_path_default.relative(docDirectory, filepath)))}"] = ${JSON.stringify(pageMeta)};
|
|
408
|
+
`;
|
|
409
|
+
return result;
|
|
410
|
+
} catch (e) {
|
|
411
|
+
if (e instanceof Error) throw e;
|
|
412
|
+
logger.error(`MDX compile error: ${e} in ${filepath}`);
|
|
413
|
+
throw new Error(`MDX compile error: ${e} in ${filepath}`);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
const cache = new Map();
|
|
417
|
+
async function compileWithCrossCompilerCache(options) {
|
|
418
|
+
const task = cache.get(options.filepath);
|
|
419
|
+
if (task) return task;
|
|
420
|
+
const promise = compile(options);
|
|
421
|
+
cache.set(options.filepath, promise);
|
|
422
|
+
return promise;
|
|
423
|
+
}
|
|
424
|
+
export { compile, compileWithCrossCompilerCache };
|
package/dist/runtime/App.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
1
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { DataContext, useLocation } from "@rspress/runtime";
|
|
3
|
+
import { Layout } from "@theme";
|
|
4
|
+
import external_react_default, { useContext, useLayoutEffect } from "react";
|
|
5
|
+
import external_virtual_global_components_default from "virtual-global-components";
|
|
6
|
+
import { initPageData } from "./initPageData.js";
|
|
7
7
|
function App() {
|
|
8
|
-
const { setData: setPageData, data } =
|
|
9
|
-
const { pathname, search } =
|
|
10
|
-
|
|
8
|
+
const { setData: setPageData, data } = useContext(DataContext);
|
|
9
|
+
const { pathname, search } = useLocation();
|
|
10
|
+
useLayoutEffect(()=>{
|
|
11
11
|
async function refetchData() {
|
|
12
12
|
try {
|
|
13
|
-
const pageData = await
|
|
13
|
+
const pageData = await initPageData(pathname);
|
|
14
14
|
setPageData?.(pageData);
|
|
15
15
|
} catch (e) {
|
|
16
16
|
console.log(e);
|
|
@@ -21,23 +21,23 @@ function App() {
|
|
|
21
21
|
pathname,
|
|
22
22
|
setPageData
|
|
23
23
|
]);
|
|
24
|
-
if (!data) return /*#__PURE__*/
|
|
24
|
+
if (!data) return /*#__PURE__*/ jsx(Fragment, {});
|
|
25
25
|
const frontmatter = data.page.frontmatter || {};
|
|
26
26
|
const GLOBAL_COMPONENTS_KEY = 'globalUIComponents';
|
|
27
27
|
const query = new URLSearchParams(search);
|
|
28
28
|
const hideGlobalUIComponents = false === frontmatter[GLOBAL_COMPONENTS_KEY] || "0" === query.get(GLOBAL_COMPONENTS_KEY);
|
|
29
|
-
return /*#__PURE__*/
|
|
29
|
+
return /*#__PURE__*/ jsxs(Fragment, {
|
|
30
30
|
children: [
|
|
31
|
-
/*#__PURE__*/
|
|
32
|
-
!hideGlobalUIComponents &&
|
|
31
|
+
/*#__PURE__*/ jsx(Layout, {}),
|
|
32
|
+
!hideGlobalUIComponents && external_virtual_global_components_default.map((componentInfo, index)=>{
|
|
33
33
|
if (Array.isArray(componentInfo)) {
|
|
34
34
|
const [component, props] = componentInfo;
|
|
35
|
-
return /*#__PURE__*/
|
|
35
|
+
return /*#__PURE__*/ external_react_default.createElement(component, {
|
|
36
36
|
key: index,
|
|
37
37
|
...props
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
|
-
return /*#__PURE__*/
|
|
40
|
+
return /*#__PURE__*/ external_react_default.createElement(componentInfo, {
|
|
41
41
|
key: index
|
|
42
42
|
});
|
|
43
43
|
})
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
const head =
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { BrowserRouter, DataContext, ThemeContext } from "@rspress/runtime";
|
|
3
|
+
import { useThemeState } from "@theme";
|
|
4
|
+
import { UnheadProvider, createHead } from "@unhead/react/client";
|
|
5
|
+
import { useMemo, useState } from "react";
|
|
6
|
+
import { App } from "./App.js";
|
|
7
|
+
const head = createHead();
|
|
8
8
|
function ClientApp({ initialPageData = null }) {
|
|
9
|
-
const [data, setData] =
|
|
10
|
-
const [theme, setTheme] =
|
|
11
|
-
return /*#__PURE__*/
|
|
12
|
-
value:
|
|
9
|
+
const [data, setData] = useState(initialPageData);
|
|
10
|
+
const [theme, setTheme] = useThemeState();
|
|
11
|
+
return /*#__PURE__*/ jsx(ThemeContext.Provider, {
|
|
12
|
+
value: useMemo(()=>({
|
|
13
13
|
theme,
|
|
14
14
|
setTheme
|
|
15
15
|
}), [
|
|
16
16
|
theme,
|
|
17
17
|
setTheme
|
|
18
18
|
]),
|
|
19
|
-
children: /*#__PURE__*/
|
|
20
|
-
value:
|
|
19
|
+
children: /*#__PURE__*/ jsx(DataContext.Provider, {
|
|
20
|
+
value: useMemo(()=>({
|
|
21
21
|
data,
|
|
22
22
|
setData
|
|
23
23
|
}), [
|
|
24
24
|
data,
|
|
25
25
|
setData
|
|
26
26
|
]),
|
|
27
|
-
children: /*#__PURE__*/
|
|
28
|
-
children: /*#__PURE__*/
|
|
27
|
+
children: /*#__PURE__*/ jsx(BrowserRouter, {
|
|
28
|
+
children: /*#__PURE__*/ jsx(UnheadProvider, {
|
|
29
29
|
head: head,
|
|
30
|
-
children: /*#__PURE__*/
|
|
30
|
+
children: /*#__PURE__*/ jsx(App, {})
|
|
31
31
|
})
|
|
32
32
|
})
|
|
33
33
|
})
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createRoot } from "react-dom/client";
|
|
3
|
+
import { ClientApp } from "./ClientApp.js";
|
|
4
4
|
const container = document.getElementById('root');
|
|
5
|
-
|
|
5
|
+
createRoot(container).render(/*#__PURE__*/ jsx(ClientApp, {}));
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { isEqualPath, pathnameToRouteService } from "@rspress/runtime";
|
|
2
|
+
import { MDX_OR_MD_REGEXP, cleanUrl } from "@rspress/shared";
|
|
3
|
+
import external_virtual_site_data_default from "virtual-site-data";
|
|
4
4
|
async function initPageData(routePath) {
|
|
5
|
-
const matchedRoute =
|
|
5
|
+
const matchedRoute = pathnameToRouteService(routePath);
|
|
6
6
|
if (matchedRoute) {
|
|
7
7
|
const mod = await matchedRoute.preload();
|
|
8
|
-
const pagePath =
|
|
8
|
+
const pagePath = cleanUrl(matchedRoute.filePath);
|
|
9
9
|
const normalize = (p)=>p.replace(/\/$/, '').toLowerCase();
|
|
10
|
-
const extractPageInfo =
|
|
10
|
+
const extractPageInfo = external_virtual_site_data_default.pages.find((page)=>isEqualPath(normalize(page.routePath), normalize(matchedRoute.path)));
|
|
11
11
|
const encodedPagePath = encodeURIComponent(pagePath);
|
|
12
12
|
const meta = mod.default.__RSPRESS_PAGE_META?.[encodedPagePath] || {};
|
|
13
|
-
const { toc = [], title = '', frontmatter = {}, ...rest } =
|
|
13
|
+
const { toc = [], title = '', frontmatter = {}, ...rest } = MDX_OR_MD_REGEXP.test(matchedRoute.filePath) ? meta : mod;
|
|
14
14
|
return {
|
|
15
|
-
siteData:
|
|
15
|
+
siteData: external_virtual_site_data_default,
|
|
16
16
|
page: {
|
|
17
17
|
...rest,
|
|
18
18
|
pagePath,
|
|
@@ -24,21 +24,21 @@ async function initPageData(routePath) {
|
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
let lang =
|
|
28
|
-
let version =
|
|
29
|
-
if (
|
|
30
|
-
const path = location.pathname.replace(
|
|
31
|
-
if (
|
|
32
|
-
const result =
|
|
27
|
+
let lang = external_virtual_site_data_default.lang || '';
|
|
28
|
+
let version = external_virtual_site_data_default.multiVersion?.default || '';
|
|
29
|
+
if (external_virtual_site_data_default.lang && 'undefined' != typeof window) {
|
|
30
|
+
const path = location.pathname.replace(external_virtual_site_data_default.base, '').split('/').slice(0, 2);
|
|
31
|
+
if (external_virtual_site_data_default.locales.length) {
|
|
32
|
+
const result = external_virtual_site_data_default.locales.find(({ lang })=>path.includes(lang));
|
|
33
33
|
if (result) lang = result.lang;
|
|
34
34
|
}
|
|
35
|
-
if (
|
|
36
|
-
const result =
|
|
35
|
+
if (external_virtual_site_data_default.multiVersion.versions) {
|
|
36
|
+
const result = external_virtual_site_data_default.multiVersion.versions.find((version)=>path.includes(version));
|
|
37
37
|
if (result) version = result;
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
return {
|
|
41
|
-
siteData:
|
|
41
|
+
siteData: external_virtual_site_data_default,
|
|
42
42
|
page: {
|
|
43
43
|
pagePath: '',
|
|
44
44
|
pageType: '404',
|
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { startTransition } from "react";
|
|
3
|
+
import { hydrateRoot } from "react-dom/client";
|
|
4
|
+
import { ClientApp } from "./ClientApp.js";
|
|
5
|
+
import { initPageData } from "./initPageData.js";
|
|
5
6
|
async function renderInBrowser() {
|
|
6
7
|
const container = document.getElementById('root');
|
|
7
|
-
const initialPageData = await
|
|
8
|
-
(
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const initialPageData = await initPageData(window.location.pathname);
|
|
9
|
+
startTransition(()=>{
|
|
10
|
+
hydrateRoot(container, /*#__PURE__*/ jsx(ClientApp, {
|
|
11
|
+
initialPageData: initialPageData
|
|
12
|
+
}), {
|
|
13
|
+
onRecoverableError (error, errorInfo) {
|
|
14
|
+
if (error instanceof Error) console.warn('hydrateRoot recoverable error:', error, errorInfo);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
11
18
|
}
|
|
12
19
|
renderInBrowser();
|