@alauda/doom 0.6.12 → 0.6.13
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/lib/plugins/reference/index.js +2 -6
- package/lib/plugins/reference/normalize-img-src.d.ts +10 -0
- package/lib/plugins/reference/normalize-img-src.js +59 -0
- package/lib/plugins/reference/remark-replace.d.ts +5 -2
- package/lib/plugins/reference/remark-replace.js +50 -18
- package/lib/plugins/reference/types.d.ts +2 -0
- package/lib/plugins/reference/utils.js +1 -0
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
2
|
import { logger } from '@rspress/shared/logger';
|
|
3
|
-
import { MD_RELEASE_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN,
|
|
3
|
+
import { MD_RELEASE_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN, remarkReplace, } from './remark-replace.js';
|
|
4
4
|
import { mdProcessor, mdxProcessor, normalizeReferenceItems } from './utils.js';
|
|
5
5
|
export const referencePlugin = ({ root, lang, localBasePath, items = [], force, releaseNotes, }) => {
|
|
6
6
|
const normalizedItems = normalizeReferenceItems(items);
|
|
@@ -21,11 +21,7 @@ export const referencePlugin = ({ root, lang, localBasePath, items = [], force,
|
|
|
21
21
|
],
|
|
22
22
|
],
|
|
23
23
|
},
|
|
24
|
-
async modifySearchIndexData(pages
|
|
25
|
-
if (!isProd) {
|
|
26
|
-
refCache.clear();
|
|
27
|
-
releaseCache.clear();
|
|
28
|
-
}
|
|
24
|
+
async modifySearchIndexData(pages) {
|
|
29
25
|
const results = await Promise.allSettled(pages.map(async (page) => {
|
|
30
26
|
const filepath = page._filepath;
|
|
31
27
|
if (!/\.mdx?$/.test(filepath)) {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Content } from 'mdast';
|
|
2
|
+
import type { NormalizedReferenceSource } from './types.js';
|
|
3
|
+
export declare const normalizeImgSrc: (content: Content, { refSource, localPublicBase, publicBase, sourceBase, targetBase, force, }: {
|
|
4
|
+
refSource: NormalizedReferenceSource;
|
|
5
|
+
localPublicBase: string;
|
|
6
|
+
publicBase: string;
|
|
7
|
+
sourceBase: string;
|
|
8
|
+
targetBase: string;
|
|
9
|
+
force?: boolean;
|
|
10
|
+
}) => Content;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { isExternalUrl, removeLeadingSlash } from '@rspress/core';
|
|
4
|
+
import { cloneDeep } from 'es-toolkit';
|
|
5
|
+
import { visit } from 'unist-util-visit';
|
|
6
|
+
const RELATIVE_URL_PATTERN = /^\.\.?\//;
|
|
7
|
+
export const normalizeImgSrc = (content, { refSource, localPublicBase, publicBase, sourceBase, targetBase, force, }) => {
|
|
8
|
+
content = cloneDeep(content);
|
|
9
|
+
const resolveImgSrc = (src, force) => {
|
|
10
|
+
if (isExternalUrl(src)) {
|
|
11
|
+
return src;
|
|
12
|
+
}
|
|
13
|
+
if (refSource.repo) {
|
|
14
|
+
const targetDir = path.resolve(localPublicBase, '_remotes', refSource.name);
|
|
15
|
+
let sourcePath;
|
|
16
|
+
let targetPath;
|
|
17
|
+
let imageSrc;
|
|
18
|
+
if (RELATIVE_URL_PATTERN.test(src)) {
|
|
19
|
+
sourcePath = path.resolve(sourceBase, src);
|
|
20
|
+
imageSrc = path.relative(sourceBase, sourcePath);
|
|
21
|
+
targetPath = path.resolve(targetDir, imageSrc);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
imageSrc = removeLeadingSlash(src);
|
|
25
|
+
sourcePath = path.resolve(publicBase, imageSrc);
|
|
26
|
+
targetPath = path.resolve(targetDir, imageSrc);
|
|
27
|
+
}
|
|
28
|
+
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
|
|
29
|
+
if (force || !fs.existsSync(targetPath)) {
|
|
30
|
+
fs.copyFileSync(sourcePath, targetPath);
|
|
31
|
+
}
|
|
32
|
+
return `/_remotes/${refSource.name}/${imageSrc}`;
|
|
33
|
+
}
|
|
34
|
+
else if (!RELATIVE_URL_PATTERN.test(src)) {
|
|
35
|
+
return src;
|
|
36
|
+
}
|
|
37
|
+
const targetSrc = path.relative(targetBase, path.resolve(sourceBase, src));
|
|
38
|
+
return RELATIVE_URL_PATTERN.test(targetSrc) ? targetSrc : `./${targetSrc}`;
|
|
39
|
+
};
|
|
40
|
+
visit(content, 'image', (node) => {
|
|
41
|
+
node.url = resolveImgSrc(node.url, force);
|
|
42
|
+
});
|
|
43
|
+
visit(content, ['mdxJsxFlowElement', 'mdxJsxTextElement'], (node_) => {
|
|
44
|
+
const node = node_;
|
|
45
|
+
if (node.name !== 'img') {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const srcNode = node.attributes.find((attr) => 'name' in attr && attr.name === 'src');
|
|
49
|
+
if (srcNode?.type !== 'mdxJsxAttribute') {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const src = srcNode.value;
|
|
53
|
+
if (typeof src !== 'string') {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
srcNode.value = resolveImgSrc(src, force);
|
|
57
|
+
});
|
|
58
|
+
return content;
|
|
59
|
+
};
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { Content, Root } from 'mdast';
|
|
2
2
|
import { type Plugin } from 'unified';
|
|
3
3
|
import type { NormalizedReferenceSource, ReleaseNotesOptions } from './types.js';
|
|
4
|
-
export
|
|
5
|
-
|
|
4
|
+
export interface ResolveReferenceResult {
|
|
5
|
+
publicBase: string;
|
|
6
|
+
sourceBase: string;
|
|
7
|
+
contents: Content[];
|
|
8
|
+
}
|
|
6
9
|
export declare const MD_RELEASE_COMMENT_PATTERN: RegExp;
|
|
7
10
|
export declare const MDX_RELEASE_COMMENT_PATTERN: RegExp;
|
|
8
11
|
export declare const maybeHaveRef: (filepath: string, content: string) => boolean | undefined;
|
|
@@ -7,17 +7,19 @@ import { render } from 'ejs';
|
|
|
7
7
|
import { simpleGit } from 'simple-git';
|
|
8
8
|
import { parse, stringify } from 'yaml';
|
|
9
9
|
import { cyan, red } from 'yoctocolors';
|
|
10
|
+
import { normalizeImgSrc } from './normalize-img-src.js';
|
|
10
11
|
import { parseToc } from './parse-toc.js';
|
|
11
12
|
import { getFrontmatterNode, mdProcessor, mdxProcessor } from './utils.js';
|
|
12
13
|
const remotesFolder = path.resolve(os.homedir(), '.doom/remotes');
|
|
13
|
-
|
|
14
|
-
const resolveReference_ = async (localBasePath, items, refName, force) => {
|
|
14
|
+
const refCache = new Map();
|
|
15
|
+
const resolveReference_ = async (localBasePath, localPublicBase, items, refName, force) => {
|
|
15
16
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
16
17
|
if (!items[refName]) {
|
|
17
18
|
logger.error(`Reference \`${red(refName)}\` not found`);
|
|
18
19
|
return;
|
|
19
20
|
}
|
|
20
21
|
const source = items[refName];
|
|
22
|
+
let publicBase;
|
|
21
23
|
let sourcePath = source.path;
|
|
22
24
|
if (source.repo) {
|
|
23
25
|
const repoFolder = path.resolve(remotesFolder, source.slug);
|
|
@@ -49,9 +51,11 @@ const resolveReference_ = async (localBasePath, items, refName, force) => {
|
|
|
49
51
|
'--allow-unrelated-histories',
|
|
50
52
|
]);
|
|
51
53
|
}
|
|
54
|
+
publicBase = path.resolve(repoFolder, source.publicBase ?? 'docs/public');
|
|
52
55
|
sourcePath = path.resolve(repoFolder, sourcePath);
|
|
53
56
|
}
|
|
54
57
|
else {
|
|
58
|
+
publicBase = localPublicBase;
|
|
55
59
|
sourcePath = path.resolve(localBasePath, sourcePath);
|
|
56
60
|
}
|
|
57
61
|
let found = false;
|
|
@@ -82,8 +86,13 @@ const resolveReference_ = async (localBasePath, items, refName, force) => {
|
|
|
82
86
|
? mdxProcessor
|
|
83
87
|
: mdProcessor;
|
|
84
88
|
const root = processor.parse(content);
|
|
89
|
+
const sourceBase = path.dirname(sourcePath);
|
|
85
90
|
if (!source.anchor) {
|
|
86
|
-
return
|
|
91
|
+
return {
|
|
92
|
+
publicBase,
|
|
93
|
+
sourceBase,
|
|
94
|
+
contents: root.children,
|
|
95
|
+
};
|
|
87
96
|
}
|
|
88
97
|
const frontmatterNode = getFrontmatterNode(root);
|
|
89
98
|
const { toc } = parseToc(root, true);
|
|
@@ -105,17 +114,26 @@ const resolveReference_ = async (localBasePath, items, refName, force) => {
|
|
|
105
114
|
}
|
|
106
115
|
nodes.push(node);
|
|
107
116
|
}
|
|
108
|
-
return
|
|
117
|
+
return {
|
|
118
|
+
publicBase,
|
|
119
|
+
sourceBase,
|
|
120
|
+
contents: nodes,
|
|
121
|
+
};
|
|
109
122
|
};
|
|
110
|
-
const resolveReference = async (localBasePath, items, refName, force) => {
|
|
123
|
+
const resolveReference = async (localBasePath, localPublicBase, items, refName, force) => {
|
|
111
124
|
if (refCache.has(refName)) {
|
|
112
125
|
return refCache.get(refName);
|
|
113
126
|
}
|
|
114
|
-
const resolving = resolveReference_(localBasePath, items, refName, force);
|
|
127
|
+
const resolving = resolveReference_(localBasePath, localPublicBase, items, refName, force);
|
|
115
128
|
refCache.set(refName, resolving);
|
|
129
|
+
if (!isProduction()) {
|
|
130
|
+
setTimeout(() => {
|
|
131
|
+
refCache.delete(refName);
|
|
132
|
+
}, 5_000);
|
|
133
|
+
}
|
|
116
134
|
return resolving;
|
|
117
135
|
};
|
|
118
|
-
|
|
136
|
+
const releaseCache = new Map();
|
|
119
137
|
const FIELD_MAPPER = {
|
|
120
138
|
zh: 'customfield_13800',
|
|
121
139
|
en: 'customfield_13801',
|
|
@@ -229,6 +247,8 @@ export const remarkReplace = function ({ lang, localBasePath, root, items, force
|
|
|
229
247
|
if (!maybeHaveRef(filepath, content)) {
|
|
230
248
|
return;
|
|
231
249
|
}
|
|
250
|
+
const localPublicBase = path.resolve(root, 'public');
|
|
251
|
+
const targetBase = path.dirname(filepath);
|
|
232
252
|
const isMdx = filepath.endsWith('.mdx');
|
|
233
253
|
const processor = isMdx ? mdxProcessor : mdProcessor;
|
|
234
254
|
const originalAst = vfile.data.original
|
|
@@ -278,12 +298,20 @@ export const remarkReplace = function ({ lang, localBasePath, root, items, force
|
|
|
278
298
|
else {
|
|
279
299
|
start = null;
|
|
280
300
|
}
|
|
281
|
-
const resolved = await resolveReference(localBasePath, items, refName, force);
|
|
301
|
+
const resolved = await resolveReference(localBasePath, localPublicBase, items, refName, force);
|
|
282
302
|
if (resolved) {
|
|
283
|
-
const
|
|
284
|
-
|
|
303
|
+
const refSource = items[refName];
|
|
304
|
+
const { frontmatterMode } = refSource;
|
|
305
|
+
for (const nodeItem of resolved.contents) {
|
|
285
306
|
if (nodeItem.type !== 'yaml') {
|
|
286
|
-
newContentChildren.push(nodeItem
|
|
307
|
+
newContentChildren.push(normalizeImgSrc(nodeItem, {
|
|
308
|
+
refSource,
|
|
309
|
+
localPublicBase,
|
|
310
|
+
targetBase,
|
|
311
|
+
publicBase: resolved.publicBase,
|
|
312
|
+
sourceBase: resolved.sourceBase,
|
|
313
|
+
force,
|
|
314
|
+
}));
|
|
287
315
|
continue;
|
|
288
316
|
}
|
|
289
317
|
if (!frontmatterMode || frontmatterMode === 'ignore') {
|
|
@@ -322,6 +350,11 @@ export const remarkReplace = function ({ lang, localBasePath, root, items, force
|
|
|
322
350
|
newAstChildren.push(releaseContent ?? node);
|
|
323
351
|
}
|
|
324
352
|
newContentChildren.push(node);
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
newAstChildren.push(node);
|
|
356
|
+
if (start == null) {
|
|
357
|
+
newContentChildren.push(node);
|
|
325
358
|
}
|
|
326
359
|
}
|
|
327
360
|
if (start != null) {
|
|
@@ -338,15 +371,14 @@ export const remarkReplace = function ({ lang, localBasePath, root, items, force
|
|
|
338
371
|
});
|
|
339
372
|
}
|
|
340
373
|
if (checkContent) {
|
|
374
|
+
const originalImports = originalAst.children
|
|
375
|
+
.filter((it) => it.type === 'mdxjsEsm')
|
|
376
|
+
.map((it) => it.value);
|
|
341
377
|
const newContent = processor.stringify({
|
|
342
378
|
...ast,
|
|
343
|
-
children: newContentChildren
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
347
|
-
const matched = n.value.match(/^import\b.+\bfrom\b\s+(['"])([^'"]+)\1$/);
|
|
348
|
-
return !matched?.[2] || !path.isAbsolute(matched[2]);
|
|
349
|
-
}),
|
|
379
|
+
children: newContentChildren
|
|
380
|
+
.flat()
|
|
381
|
+
.filter((n) => n.type !== 'mdxjsEsm' || originalImports.includes(n.value)),
|
|
350
382
|
});
|
|
351
383
|
if (content !== newContent) {
|
|
352
384
|
await fs.writeFile(filepath, newContent);
|
|
@@ -11,10 +11,12 @@ export interface ReferenceItemSource {
|
|
|
11
11
|
}
|
|
12
12
|
export interface ReferenceItem {
|
|
13
13
|
repo?: string;
|
|
14
|
+
publicBase?: string;
|
|
14
15
|
sources: ReferenceItemSource[];
|
|
15
16
|
}
|
|
16
17
|
export interface NormalizedReferenceSource extends ReferenceItemSource {
|
|
17
18
|
repo?: string;
|
|
19
|
+
publicBase?: string;
|
|
18
20
|
slug?: string;
|
|
19
21
|
anchor?: string;
|
|
20
22
|
}
|