@alauda/doom 0.7.2 → 0.7.3
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/cli/load-config.js +2 -2
- package/lib/plugins/replace/index.d.ts +10 -0
- package/lib/plugins/replace/index.js +62 -0
- package/lib/plugins/replace/normalize-img-src.d.ts +10 -0
- package/lib/plugins/replace/normalize-img-src.js +59 -0
- package/lib/plugins/replace/parse-toc.d.ts +17 -0
- package/lib/plugins/replace/parse-toc.js +51 -0
- package/lib/plugins/replace/remark-jsx-table.d.ts +3 -0
- package/lib/plugins/replace/remark-jsx-table.js +10 -0
- package/lib/plugins/replace/remark-replace.d.ts +16 -0
- package/lib/plugins/replace/remark-replace.js +172 -0
- package/lib/plugins/replace/resolve-reference.d.ts +8 -0
- package/lib/plugins/replace/resolve-reference.js +134 -0
- package/lib/plugins/replace/resolve-release.d.ts +2 -0
- package/lib/plugins/replace/resolve-release.js +93 -0
- package/lib/plugins/replace/types.d.ts +32 -0
- package/lib/plugins/replace/types.js +1 -0
- package/lib/plugins/replace/utils.d.ts +14 -0
- package/lib/plugins/replace/utils.js +85 -0
- package/lib/utils/types.d.ts +1 -1
- package/package.json +1 -1
package/lib/cli/load-config.js
CHANGED
|
@@ -9,7 +9,7 @@ import { apiPlugin, autoSidebarPlugin, createTransformerCallouts, globalPlugin,
|
|
|
9
9
|
import { normalizeVersion } from '../shared/index.js';
|
|
10
10
|
import { pkgResolve, resolveStaticConfig, } from '../utils/index.js';
|
|
11
11
|
import { CWD, DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS, I18N_FILE, SITES_FILE, YAML_EXTENSIONS, } from './constants.js';
|
|
12
|
-
import {
|
|
12
|
+
import { replacePlugin } from '../plugins/replace/index.js';
|
|
13
13
|
const DEFAULT_LOGO = '/logo.svg';
|
|
14
14
|
const zhLocaleConfig = {
|
|
15
15
|
searchPlaceholderText: '搜索文档',
|
|
@@ -62,7 +62,7 @@ const getCommonConfig = (config, cliRoot, configFilePath, version, force) => {
|
|
|
62
62
|
sites: config.sites,
|
|
63
63
|
version,
|
|
64
64
|
}),
|
|
65
|
-
|
|
65
|
+
replacePlugin({
|
|
66
66
|
root,
|
|
67
67
|
lang: fallbackToZh ? null : (config.lang ?? 'en'),
|
|
68
68
|
localBasePath: configFilePath ? path.dirname(configFilePath) : root,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RspressPlugin } from '@rspress/core';
|
|
2
|
+
import type { ReferenceItem, ReleaseNotesOptions } from './types.js';
|
|
3
|
+
export declare const replacePlugin: ({ root, lang, localBasePath, items, force, releaseNotes, }: {
|
|
4
|
+
root: string;
|
|
5
|
+
lang: string | null;
|
|
6
|
+
localBasePath: string;
|
|
7
|
+
items?: ReferenceItem[];
|
|
8
|
+
force?: boolean;
|
|
9
|
+
releaseNotes?: ReleaseNotesOptions;
|
|
10
|
+
}) => RspressPlugin;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { logger } from '@rspress/shared/logger';
|
|
3
|
+
import { remarkJsxTable } from './remark-jsx-table.js';
|
|
4
|
+
import { MD_RELEASE_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN, remarkReplace, } from './remark-replace.js';
|
|
5
|
+
import { mdProcessor, mdxProcessor, normalizeReferenceItems } from './utils.js';
|
|
6
|
+
export const replacePlugin = ({ root, lang, localBasePath, items = [], force, releaseNotes, }) => {
|
|
7
|
+
const normalizedItems = normalizeReferenceItems(items);
|
|
8
|
+
return {
|
|
9
|
+
name: 'doom-replace',
|
|
10
|
+
markdown: {
|
|
11
|
+
remarkPlugins: [
|
|
12
|
+
[
|
|
13
|
+
remarkReplace,
|
|
14
|
+
{
|
|
15
|
+
lang,
|
|
16
|
+
localBasePath,
|
|
17
|
+
root,
|
|
18
|
+
items: normalizedItems,
|
|
19
|
+
releaseNotes,
|
|
20
|
+
force,
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
remarkJsxTable,
|
|
24
|
+
],
|
|
25
|
+
},
|
|
26
|
+
async modifySearchIndexData(pages) {
|
|
27
|
+
const results = await Promise.allSettled(pages.map(async (page) => {
|
|
28
|
+
const filepath = page._filepath;
|
|
29
|
+
if (!/\.mdx?$/.test(filepath)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const isMdx = filepath.endsWith('.mdx');
|
|
33
|
+
const content = await fs.readFile(filepath, 'utf8');
|
|
34
|
+
if (!(isMdx ? MDX_RELEASE_COMMENT_PATTERN : MD_RELEASE_COMMENT_PATTERN).test(content)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const processor = isMdx ? mdxProcessor : mdProcessor;
|
|
38
|
+
const compiler = processor().data('pageMeta', {}).use(remarkReplace, {
|
|
39
|
+
lang,
|
|
40
|
+
localBasePath,
|
|
41
|
+
root,
|
|
42
|
+
items: normalizedItems,
|
|
43
|
+
force,
|
|
44
|
+
releaseNotes,
|
|
45
|
+
});
|
|
46
|
+
const vfile = await compiler.process({
|
|
47
|
+
path: filepath,
|
|
48
|
+
value: content,
|
|
49
|
+
data: {
|
|
50
|
+
original: true,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
page.content = vfile.toString();
|
|
54
|
+
}));
|
|
55
|
+
for (const result of results) {
|
|
56
|
+
if (result.status === 'rejected') {
|
|
57
|
+
logger.error(result.reason);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Root } from 'mdast';
|
|
2
|
+
export interface TocItem {
|
|
3
|
+
id: string;
|
|
4
|
+
text: string;
|
|
5
|
+
depth: number;
|
|
6
|
+
index: number;
|
|
7
|
+
}
|
|
8
|
+
export interface PageMeta {
|
|
9
|
+
toc: TocItem[];
|
|
10
|
+
title: string;
|
|
11
|
+
headingTitle: string;
|
|
12
|
+
frontmatter?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
export declare const parseToc: (tree: Root, allDepths?: boolean) => {
|
|
15
|
+
title: string;
|
|
16
|
+
toc: TocItem[];
|
|
17
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Slugger from 'github-slugger';
|
|
2
|
+
import { visitChildren } from 'unist-util-visit-children';
|
|
3
|
+
import { extractTextAndId } from './utils.js';
|
|
4
|
+
export const parseToc = (tree, allDepths) => {
|
|
5
|
+
let title = '';
|
|
6
|
+
const toc = [];
|
|
7
|
+
const slugger = new Slugger();
|
|
8
|
+
visitChildren((node, index) => {
|
|
9
|
+
if (node.type !== 'heading' || !node.depth || !node.children) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
// Collect h1 ~ h4 by default
|
|
13
|
+
if (allDepths || node.depth < 5) {
|
|
14
|
+
let customId = '';
|
|
15
|
+
const text = node.children
|
|
16
|
+
.map((child) => {
|
|
17
|
+
if (child.type === 'link') {
|
|
18
|
+
return child.children?.map((item) => item.value).join('');
|
|
19
|
+
}
|
|
20
|
+
if (child.type === 'strong') {
|
|
21
|
+
return `**${child.children?.map((item) => item.value).join('')}**`;
|
|
22
|
+
}
|
|
23
|
+
if (child.type === 'text') {
|
|
24
|
+
const [textPart, idPart] = extractTextAndId(child.value);
|
|
25
|
+
customId = idPart;
|
|
26
|
+
return textPart;
|
|
27
|
+
}
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
29
|
+
if (child.type === 'inlineCode') {
|
|
30
|
+
return `\`${child.value}\``;
|
|
31
|
+
}
|
|
32
|
+
return '';
|
|
33
|
+
})
|
|
34
|
+
.join('');
|
|
35
|
+
if (!allDepths && node.depth === 1) {
|
|
36
|
+
if (!title) {
|
|
37
|
+
title = text;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
const id = customId ? customId : slugger.slug(text);
|
|
42
|
+
const { depth } = node;
|
|
43
|
+
toc.push({ id, text, depth, index });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
})(tree);
|
|
47
|
+
return {
|
|
48
|
+
title,
|
|
49
|
+
toc,
|
|
50
|
+
};
|
|
51
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { visit } from 'unist-util-visit';
|
|
2
|
+
export const remarkJsxTable = () => (ast) => {
|
|
3
|
+
visit(ast, 'mdxJsxFlowElement', (node) => {
|
|
4
|
+
if (node.name &&
|
|
5
|
+
['table', 'tr', 'th', 'td'].includes(node.name) &&
|
|
6
|
+
node.data?._mdxExplicitJsx) {
|
|
7
|
+
delete node.data._mdxExplicitJsx;
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Root } from 'mdast';
|
|
2
|
+
import { type Plugin } from 'unified';
|
|
3
|
+
import type { NormalizedReferenceSource, ReleaseNotesOptions } from './types.js';
|
|
4
|
+
export declare const MD_RELEASE_COMMENT_PATTERN: RegExp;
|
|
5
|
+
export declare const MDX_RELEASE_COMMENT_PATTERN: RegExp;
|
|
6
|
+
export declare const maybeHaveRef: (filepath: string, content: string) => boolean | undefined;
|
|
7
|
+
export declare const remarkReplace: Plugin<[
|
|
8
|
+
{
|
|
9
|
+
lang: string | null;
|
|
10
|
+
localBasePath: string;
|
|
11
|
+
root: string;
|
|
12
|
+
items: Record<string, NormalizedReferenceSource>;
|
|
13
|
+
force?: boolean;
|
|
14
|
+
releaseNotes?: ReleaseNotesOptions;
|
|
15
|
+
}
|
|
16
|
+
], Root>;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { isProduction } from '@rspress/core';
|
|
4
|
+
import { logger } from '@rspress/shared/logger';
|
|
5
|
+
import { parse, stringify } from 'yaml';
|
|
6
|
+
import { cyan, red } from 'yoctocolors';
|
|
7
|
+
import { normalizeImgSrc } from './normalize-img-src.js';
|
|
8
|
+
import { getFrontmatterNode, mdProcessor, mdxProcessor } from './utils.js';
|
|
9
|
+
import { resolveReference } from './resolve-reference.js';
|
|
10
|
+
import { resolveRelease } from './resolve-release.js';
|
|
11
|
+
const MD_REF_START_COMMENT_PATTERN = /<!-{2,} *reference-start#(.+) *-{2,}>/;
|
|
12
|
+
const MDX_REF_START_COMMENT_PATTERN = /{\/\*+ *reference-start#(.+) *\*+\/}/;
|
|
13
|
+
const MDX_REF_START_PATTERN = /\/\*+ *reference-start#(.+) *\*+\//;
|
|
14
|
+
const MD_REF_END_COMMENT_PATTERN = /<!-{2,} *reference-end *-{2,}>/;
|
|
15
|
+
const MDX_REF_END_PATTERN = /\/\*+ *reference-end *\*+\//;
|
|
16
|
+
export const MD_RELEASE_COMMENT_PATTERN = /<!-{2,} *release-notes-for-bugs\?(.+) *-{2,}>/;
|
|
17
|
+
export const MDX_RELEASE_COMMENT_PATTERN = /{\/\*+ *release-notes-for-bugs\?(.+) *\*+\/}/;
|
|
18
|
+
const MDX_RELEASE_PATTERN = /\/\*+ *release-notes-for-bugs\?(.+) *\*+\//;
|
|
19
|
+
export const maybeHaveRef = (filepath, content) => {
|
|
20
|
+
if (!/\.mdx?$/.test(filepath)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
return (filepath.endsWith('.mdx')
|
|
24
|
+
? [MDX_REF_START_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN]
|
|
25
|
+
: [MD_REF_START_COMMENT_PATTERN, MD_RELEASE_COMMENT_PATTERN]).some((p) => p.test(content));
|
|
26
|
+
};
|
|
27
|
+
const { CI } = process.env;
|
|
28
|
+
const isCi = CI !== 'false' && !!CI;
|
|
29
|
+
export const remarkReplace = function ({ lang, localBasePath, root, items, force, releaseNotes }) {
|
|
30
|
+
return async (ast, vfile) => {
|
|
31
|
+
const filepath = vfile.path;
|
|
32
|
+
let content = vfile.toString();
|
|
33
|
+
if (!maybeHaveRef(filepath, content)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const localPublicBase = path.resolve(root, 'public');
|
|
37
|
+
const targetBase = path.dirname(filepath);
|
|
38
|
+
const isMdx = filepath.endsWith('.mdx');
|
|
39
|
+
const processor = isMdx ? mdxProcessor : mdProcessor;
|
|
40
|
+
const originalAst = vfile.data.original
|
|
41
|
+
? ast
|
|
42
|
+
: processor.parse((content = await fs.readFile(filepath, 'utf8')));
|
|
43
|
+
const frontmatterNode = getFrontmatterNode(originalAst);
|
|
44
|
+
let frontmatter = frontmatterNode &&
|
|
45
|
+
parse(frontmatterNode.value);
|
|
46
|
+
const newContentChildren = [];
|
|
47
|
+
let start = false;
|
|
48
|
+
let refName = '';
|
|
49
|
+
let matched;
|
|
50
|
+
let checkContent = false;
|
|
51
|
+
const relativePath = path.relative(root, filepath);
|
|
52
|
+
for (const node of originalAst.children) {
|
|
53
|
+
if (node.type !== (isMdx ? 'mdxFlowExpression' : 'html')) {
|
|
54
|
+
if (node.type === 'yaml') {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (!start) {
|
|
58
|
+
newContentChildren.push(node);
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if ((matched = node.value.match(isMdx ? MDX_REF_START_PATTERN : MD_REF_START_COMMENT_PATTERN))) {
|
|
63
|
+
if (start) {
|
|
64
|
+
logger.warn(`Invalid reference start block ${red(node.value)}, nested reference blocks are not allowed`);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
checkContent = true;
|
|
68
|
+
start = true;
|
|
69
|
+
refName = matched[1].trim();
|
|
70
|
+
}
|
|
71
|
+
newContentChildren.push(node);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if ((isMdx ? MDX_REF_END_PATTERN : MD_REF_END_COMMENT_PATTERN).test(node.value)) {
|
|
75
|
+
if (start) {
|
|
76
|
+
start = false;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
logger.warn(`Invalid reference end block ${red(node.value)}, no matching start block found`);
|
|
80
|
+
}
|
|
81
|
+
const resolved = await resolveReference(localBasePath, localPublicBase, items, refName, force);
|
|
82
|
+
if (resolved) {
|
|
83
|
+
const refSource = items[refName];
|
|
84
|
+
const { frontmatterMode } = refSource;
|
|
85
|
+
for (const nodeItem of resolved.contents) {
|
|
86
|
+
if (nodeItem.type !== 'yaml') {
|
|
87
|
+
newContentChildren.push(normalizeImgSrc(nodeItem, {
|
|
88
|
+
refSource,
|
|
89
|
+
localPublicBase,
|
|
90
|
+
targetBase,
|
|
91
|
+
publicBase: resolved.publicBase,
|
|
92
|
+
sourceBase: resolved.sourceBase,
|
|
93
|
+
force,
|
|
94
|
+
}));
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (!frontmatterMode || frontmatterMode === 'ignore') {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (frontmatterMode === 'remove') {
|
|
101
|
+
frontmatter = null;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const data = parse(nodeItem.value);
|
|
105
|
+
if (frontmatterMode === 'merge') {
|
|
106
|
+
if (data) {
|
|
107
|
+
frontmatter = {
|
|
108
|
+
...frontmatter,
|
|
109
|
+
...data,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
113
|
+
}
|
|
114
|
+
else if (frontmatterMode === 'replace') {
|
|
115
|
+
frontmatter = data;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
newContentChildren.push(node);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (!start) {
|
|
123
|
+
newContentChildren.push(node);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (start) {
|
|
127
|
+
logger.warn(`Invalid reference start block ${red(refName)}, no matching end block found, adding for you`);
|
|
128
|
+
newContentChildren.push({
|
|
129
|
+
type: isMdx ? 'mdxFlowExpression' : 'html',
|
|
130
|
+
value: isMdx ? '/* reference-end */' : '<!-- reference-end -->',
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (frontmatter) {
|
|
134
|
+
newContentChildren.unshift({
|
|
135
|
+
type: 'yaml',
|
|
136
|
+
value: stringify(frontmatter).trim(),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (checkContent) {
|
|
140
|
+
const newContent = processor.stringify({
|
|
141
|
+
...ast,
|
|
142
|
+
children: newContentChildren.flat(),
|
|
143
|
+
});
|
|
144
|
+
if (content !== newContent) {
|
|
145
|
+
await fs.writeFile(filepath, newContent);
|
|
146
|
+
if (!vfile.data.original && isProduction()) {
|
|
147
|
+
const message = `Reference block in \`${cyan(relativePath)}\` has been updated, please commit the changes`;
|
|
148
|
+
if (isCi) {
|
|
149
|
+
process.env.__DOOM_REBUILD__ = 'true';
|
|
150
|
+
}
|
|
151
|
+
logger.warn(message);
|
|
152
|
+
}
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const newAstChildren = [];
|
|
157
|
+
const currLang = lang === null ? 'zh' : relativePath.split('/')[0];
|
|
158
|
+
for (const node of ast.children) {
|
|
159
|
+
if (node.type !== (isMdx ? 'mdxFlowExpression' : 'html')) {
|
|
160
|
+
newAstChildren.push(node);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if ((matched = node.value.match(isMdx ? MDX_RELEASE_PATTERN : MD_RELEASE_COMMENT_PATTERN))) {
|
|
164
|
+
const releaseContent = await resolveRelease(releaseNotes?.queryTemplates ?? {}, matched[1].trim(), currLang);
|
|
165
|
+
newAstChildren.push(releaseContent ?? node);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
newAstChildren.push(node);
|
|
169
|
+
}
|
|
170
|
+
ast.children = newAstChildren.flat();
|
|
171
|
+
};
|
|
172
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Content } from 'mdast';
|
|
2
|
+
import type { NormalizedReferenceSource } from './types.js';
|
|
3
|
+
export interface ResolveReferenceResult {
|
|
4
|
+
publicBase: string;
|
|
5
|
+
sourceBase: string;
|
|
6
|
+
contents: Content[];
|
|
7
|
+
}
|
|
8
|
+
export declare const resolveReference: (localBasePath: string, localPublicBase: string, items: Record<string, NormalizedReferenceSource>, refName: string, force?: boolean) => Promise<ResolveReferenceResult | undefined>;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { isProduction } from '@rspress/core';
|
|
5
|
+
import { logger } from '@rspress/shared/logger';
|
|
6
|
+
import { render } from 'ejs';
|
|
7
|
+
import { simpleGit } from 'simple-git';
|
|
8
|
+
import { cyan, red } from 'yoctocolors';
|
|
9
|
+
import { parseToc } from './parse-toc.js';
|
|
10
|
+
import { getFrontmatterNode, mdProcessor, mdxProcessor } from './utils.js';
|
|
11
|
+
const remotesFolder = path.resolve(os.homedir(), '.doom/remotes');
|
|
12
|
+
const refCache = new Map();
|
|
13
|
+
const resolveReference_ = async (localBasePath, localPublicBase, items, refName, force) => {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
15
|
+
if (!items[refName]) {
|
|
16
|
+
logger.error(`Reference \`${red(refName)}\` not found`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const source = items[refName];
|
|
20
|
+
let publicBase;
|
|
21
|
+
let sourcePath = source.path;
|
|
22
|
+
if (source.repo) {
|
|
23
|
+
const repoFolder = path.resolve(remotesFolder, source.slug);
|
|
24
|
+
let created = false;
|
|
25
|
+
try {
|
|
26
|
+
const stat = await fs.stat(path.resolve(repoFolder, '.git'));
|
|
27
|
+
if (stat.isDirectory()) {
|
|
28
|
+
created = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// ignore
|
|
33
|
+
}
|
|
34
|
+
if (!created) {
|
|
35
|
+
await fs.mkdir(repoFolder, { recursive: true });
|
|
36
|
+
}
|
|
37
|
+
const git = simpleGit(repoFolder);
|
|
38
|
+
if (!created) {
|
|
39
|
+
logger.info(`Cloning remote \`${cyan(source.slug)}\` repository...`);
|
|
40
|
+
await git.clone(source.repo, repoFolder, ['--depth', '1']);
|
|
41
|
+
}
|
|
42
|
+
if (force) {
|
|
43
|
+
logger.info(`Pulling latest changes for \`${cyan(source.slug)}\`...`);
|
|
44
|
+
await git.pull([
|
|
45
|
+
'--depth',
|
|
46
|
+
'1',
|
|
47
|
+
'--force',
|
|
48
|
+
'--rebase',
|
|
49
|
+
'--allow-unrelated-histories',
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
publicBase = path.resolve(repoFolder, source.publicBase ?? 'docs/public');
|
|
53
|
+
sourcePath = path.resolve(repoFolder, sourcePath);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
publicBase = localPublicBase;
|
|
57
|
+
sourcePath = path.resolve(localBasePath, sourcePath);
|
|
58
|
+
}
|
|
59
|
+
let found = false;
|
|
60
|
+
try {
|
|
61
|
+
const stat = await fs.stat(sourcePath);
|
|
62
|
+
found = stat.isFile();
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
//
|
|
66
|
+
}
|
|
67
|
+
if (!found) {
|
|
68
|
+
logger.error(`Reference path \`${red(source.path)}\` for \`${red(source.name)}\` not found`);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
let content = await fs.readFile(sourcePath, 'utf8');
|
|
72
|
+
for (const processor of source.processors ?? []) {
|
|
73
|
+
switch (processor.type) {
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
75
|
+
case 'ejsTemplate': {
|
|
76
|
+
content = await render(content, { data: processor.data }, { async: true });
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const processor = (sourcePath.endsWith('.ejs')
|
|
82
|
+
? sourcePath.slice(0, -4)
|
|
83
|
+
: sourcePath).endsWith('.mdx')
|
|
84
|
+
? mdxProcessor
|
|
85
|
+
: mdProcessor;
|
|
86
|
+
const root = processor.parse(content);
|
|
87
|
+
const sourceBase = path.dirname(sourcePath);
|
|
88
|
+
if (!source.anchor) {
|
|
89
|
+
return {
|
|
90
|
+
publicBase,
|
|
91
|
+
sourceBase,
|
|
92
|
+
contents: root.children,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const frontmatterNode = getFrontmatterNode(root);
|
|
96
|
+
const { toc } = parseToc(root, true);
|
|
97
|
+
const active = toc.find((it) => it.id === source.anchor) ??
|
|
98
|
+
toc.find((it) => it.text === source.anchor);
|
|
99
|
+
if (!active) {
|
|
100
|
+
logger.error(`Anchor \`${red(source.anchor)}\` not found in \`${red(source.name)}\``);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const nodes = frontmatterNode ? [frontmatterNode] : [];
|
|
104
|
+
for (let i = active.index, n = root.children.length; i < n; i++) {
|
|
105
|
+
if (i === active.index && (source.ignoreHeading ?? active.depth === 1)) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const node = root.children[i];
|
|
109
|
+
if (node.type === 'heading' &&
|
|
110
|
+
node.depth <= active.depth &&
|
|
111
|
+
i > active.index) {
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
nodes.push(node);
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
publicBase,
|
|
118
|
+
sourceBase,
|
|
119
|
+
contents: nodes,
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
export const resolveReference = async (localBasePath, localPublicBase, items, refName, force) => {
|
|
123
|
+
if (refCache.has(refName)) {
|
|
124
|
+
return refCache.get(refName);
|
|
125
|
+
}
|
|
126
|
+
const resolving = resolveReference_(localBasePath, localPublicBase, items, refName, force);
|
|
127
|
+
refCache.set(refName, resolving);
|
|
128
|
+
if (!isProduction()) {
|
|
129
|
+
setTimeout(() => {
|
|
130
|
+
refCache.delete(refName);
|
|
131
|
+
}, 5_000);
|
|
132
|
+
}
|
|
133
|
+
return resolving;
|
|
134
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { logger } from '@rspress/shared/logger';
|
|
2
|
+
import { render } from 'ejs';
|
|
3
|
+
import { cyan, red } from 'yoctocolors';
|
|
4
|
+
const releaseCache = new Map();
|
|
5
|
+
const FIELD_MAPPER = {
|
|
6
|
+
zh: 'customfield_13800',
|
|
7
|
+
en: 'customfield_13801',
|
|
8
|
+
};
|
|
9
|
+
const { JIRA_TOKEN, JIRA_USERNAME, JIRA_PASSWORD } = process.env;
|
|
10
|
+
const issuesToMdast = (issues, lang) => {
|
|
11
|
+
return issues
|
|
12
|
+
.map((issue) => {
|
|
13
|
+
const description = ((lang !== 'en' && issue.fields[FIELD_MAPPER[lang]]) ||
|
|
14
|
+
issue.fields[FIELD_MAPPER.en])?.trim();
|
|
15
|
+
if (!description) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
type: 'listItem',
|
|
20
|
+
children: [
|
|
21
|
+
{
|
|
22
|
+
type: 'paragraph',
|
|
23
|
+
children: description
|
|
24
|
+
.split('\n')
|
|
25
|
+
.map((line) => {
|
|
26
|
+
return {
|
|
27
|
+
type: 'text',
|
|
28
|
+
value: line,
|
|
29
|
+
};
|
|
30
|
+
})
|
|
31
|
+
.reduce((acc, curr, index) => {
|
|
32
|
+
if (index === 0) {
|
|
33
|
+
return [curr];
|
|
34
|
+
}
|
|
35
|
+
return acc.concat({ type: 'html', value: '<br>' }, curr);
|
|
36
|
+
}, []),
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
})
|
|
41
|
+
.filter((_) => !!_);
|
|
42
|
+
};
|
|
43
|
+
const resolveRelease_ = async (releaseTemplates, releaseQuery) => {
|
|
44
|
+
const query = new URLSearchParams(releaseQuery);
|
|
45
|
+
const templateName = query.get('template');
|
|
46
|
+
if (!templateName) {
|
|
47
|
+
logger.error(`Release notes template not found for query \`${red(releaseQuery)}\``);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const template = releaseTemplates[templateName];
|
|
51
|
+
if (!template) {
|
|
52
|
+
logger.error(`Release notes template \`${red(templateName)}\` not found`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
let Authorization;
|
|
56
|
+
if (JIRA_TOKEN) {
|
|
57
|
+
Authorization = `Bearer ${JIRA_TOKEN}`;
|
|
58
|
+
}
|
|
59
|
+
else if (JIRA_USERNAME && JIRA_PASSWORD) {
|
|
60
|
+
Authorization = `Basic ${Buffer.from(`${JIRA_USERNAME}:${JIRA_PASSWORD}`).toString('base64')}`;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
throw new Error('`JIRA_TOKEN` or `JIRA_USERNAME` and `JIRA_PASSWORD` environments must be set for fetching Jira issues');
|
|
64
|
+
}
|
|
65
|
+
const data = {};
|
|
66
|
+
for (const [key, value] of query.entries()) {
|
|
67
|
+
if (key === 'template') {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
data[key] = value;
|
|
71
|
+
}
|
|
72
|
+
const jql = await render(template, data, { async: true });
|
|
73
|
+
logger.info(`Fetching release notes for query \`${cyan(releaseQuery)}\``);
|
|
74
|
+
const res = await fetch(`https://jira.alauda.cn/rest/api/2/search?${new URLSearchParams({ jql })}`, { headers: { Authorization } });
|
|
75
|
+
if (!res.ok) {
|
|
76
|
+
logger.error(`Failed to fetch release notes for query \`${red(releaseQuery)}\` with status \`${res.status}\``);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const { issues } = (await res.json());
|
|
80
|
+
return ['en', 'zh'].reduce((acc, curr) => Object.assign(acc, {
|
|
81
|
+
[curr]: { type: 'list', children: issuesToMdast(issues, curr) },
|
|
82
|
+
}), {});
|
|
83
|
+
};
|
|
84
|
+
export const resolveRelease = async (releaseTemplates, releaseQuery, lang) => {
|
|
85
|
+
if (releaseCache.has(releaseQuery)) {
|
|
86
|
+
const cached = await releaseCache.get(releaseQuery);
|
|
87
|
+
return cached?.[lang] ?? cached?.en;
|
|
88
|
+
}
|
|
89
|
+
const resolving = resolveRelease_(releaseTemplates, releaseQuery);
|
|
90
|
+
releaseCache.set(releaseQuery, resolving);
|
|
91
|
+
const resolved = await resolving;
|
|
92
|
+
return resolved?.[lang] ?? resolved?.en;
|
|
93
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ContentProcessor } from '../../utils/types.js';
|
|
2
|
+
export interface ReferenceItemSource {
|
|
3
|
+
name: string;
|
|
4
|
+
path: string;
|
|
5
|
+
ignoreHeading?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* @default 'ignore'
|
|
8
|
+
*/
|
|
9
|
+
frontmatterMode?: 'ignore' | 'merge' | 'remove' | 'replace';
|
|
10
|
+
processors?: ContentProcessor[];
|
|
11
|
+
}
|
|
12
|
+
export interface ReferenceItem {
|
|
13
|
+
repo?: string;
|
|
14
|
+
publicBase?: string;
|
|
15
|
+
sources: ReferenceItemSource[];
|
|
16
|
+
}
|
|
17
|
+
export interface NormalizedReferenceSource extends ReferenceItemSource {
|
|
18
|
+
repo?: string;
|
|
19
|
+
publicBase?: string;
|
|
20
|
+
slug?: string;
|
|
21
|
+
anchor?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface ReleaseNotesOptions {
|
|
24
|
+
queryTemplates: Record<string, string>;
|
|
25
|
+
}
|
|
26
|
+
export interface JiraIssue {
|
|
27
|
+
expand: string;
|
|
28
|
+
id: string;
|
|
29
|
+
self: string;
|
|
30
|
+
key: string;
|
|
31
|
+
fields: Partial<Record<string, string>>;
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Root, YAML } from 'mdast';
|
|
2
|
+
import type { MdxjsEsm } from 'mdast-util-mdxjs-esm';
|
|
3
|
+
import { type Options } from 'remark-stringify';
|
|
4
|
+
import type { NormalizedReferenceSource, ReferenceItem } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* hello world {#custom-id} -> { text: 'hello world', id: 'custom-id' }
|
|
7
|
+
*/
|
|
8
|
+
export declare const extractTextAndId: (title: string) => string[];
|
|
9
|
+
export declare const normalizeReferenceItems: (items: ReferenceItem[]) => Record<string, NormalizedReferenceSource>;
|
|
10
|
+
export declare const getASTNodeImport: (name: string, from: string) => MdxjsEsm;
|
|
11
|
+
export declare const stringifySettings: Options;
|
|
12
|
+
export declare const mdProcessor: import("unified").FrozenProcessor<Root, Root, Root, string>;
|
|
13
|
+
export declare const mdxProcessor: import("unified").FrozenProcessor<Root, Root, Root, string>;
|
|
14
|
+
export declare const getFrontmatterNode: (ast: Root) => YAML | undefined;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { logger } from '@rspress/shared/logger';
|
|
2
|
+
import remarkFrontmatter from 'remark-frontmatter';
|
|
3
|
+
import remarkGfm from 'remark-gfm';
|
|
4
|
+
import remarkMdx from 'remark-mdx';
|
|
5
|
+
import remarkParse from 'remark-parse';
|
|
6
|
+
import remarkStringify from 'remark-stringify';
|
|
7
|
+
import { unified } from 'unified';
|
|
8
|
+
import { red } from 'yoctocolors';
|
|
9
|
+
/**
|
|
10
|
+
* hello world {#custom-id} -> { text: 'hello world', id: 'custom-id' }
|
|
11
|
+
*/
|
|
12
|
+
export const extractTextAndId = (title) => {
|
|
13
|
+
const customIdReg = /{#.*}/;
|
|
14
|
+
const text = title.replace(customIdReg, '').trimEnd();
|
|
15
|
+
const customId = title.match(customIdReg)?.[0]?.slice(2, -1) || '';
|
|
16
|
+
return [text, customId];
|
|
17
|
+
};
|
|
18
|
+
export const normalizeReferenceItems = (items) => items.reduce((acc, curr) => {
|
|
19
|
+
for (const source of curr.sources) {
|
|
20
|
+
if (source.name in acc) {
|
|
21
|
+
logger.error(`Duplicate source name \`${red(source.name)}\` will be deduplicated`);
|
|
22
|
+
}
|
|
23
|
+
const repo = curr.repo;
|
|
24
|
+
const extra = {};
|
|
25
|
+
if (repo) {
|
|
26
|
+
const url = new URL(repo, 'https://gitlab-ce.alauda.cn');
|
|
27
|
+
extra.repo = url.toString();
|
|
28
|
+
extra.slug = url.pathname.slice(1);
|
|
29
|
+
}
|
|
30
|
+
const [path, anchor] = source.path.split('#');
|
|
31
|
+
acc[source.name] = {
|
|
32
|
+
...source,
|
|
33
|
+
...extra,
|
|
34
|
+
publicBase: curr.publicBase,
|
|
35
|
+
path,
|
|
36
|
+
anchor,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return acc;
|
|
40
|
+
}, {});
|
|
41
|
+
// Construct import statement for AST
|
|
42
|
+
// Such as: import image1 from './test.png'
|
|
43
|
+
export const getASTNodeImport = (name, from) => ({
|
|
44
|
+
type: 'mdxjsEsm',
|
|
45
|
+
value: `import ${name} from ${JSON.stringify(from)}`,
|
|
46
|
+
data: {
|
|
47
|
+
estree: {
|
|
48
|
+
type: 'Program',
|
|
49
|
+
sourceType: 'module',
|
|
50
|
+
body: [
|
|
51
|
+
{
|
|
52
|
+
type: 'ImportDeclaration',
|
|
53
|
+
specifiers: [
|
|
54
|
+
{
|
|
55
|
+
type: 'ImportDefaultSpecifier',
|
|
56
|
+
local: { type: 'Identifier', name },
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
source: {
|
|
60
|
+
type: 'Literal',
|
|
61
|
+
value: from,
|
|
62
|
+
raw: JSON.stringify(from),
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
export const stringifySettings = {
|
|
70
|
+
bullet: '-',
|
|
71
|
+
listItemIndent: 'one',
|
|
72
|
+
rule: '-',
|
|
73
|
+
tightDefinitions: true,
|
|
74
|
+
};
|
|
75
|
+
export const mdProcessor = unified()
|
|
76
|
+
.use(remarkParse)
|
|
77
|
+
.use(remarkFrontmatter)
|
|
78
|
+
.use(remarkGfm)
|
|
79
|
+
.use(remarkStringify, stringifySettings)
|
|
80
|
+
.freeze();
|
|
81
|
+
export const mdxProcessor = mdProcessor().use(remarkMdx).freeze();
|
|
82
|
+
export const getFrontmatterNode = (ast) => {
|
|
83
|
+
const firstNode = ast.children[0];
|
|
84
|
+
return firstNode.type === 'yaml' ? firstNode : undefined;
|
|
85
|
+
};
|
package/lib/utils/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { UserConfig } from '@rspress/core';
|
|
2
2
|
import type { ApiPluginOptions } from '../plugins/api/types.js';
|
|
3
3
|
import type { AutoSidebarPluginOptions } from '../plugins/index.js';
|
|
4
|
-
import type { ReferenceItem, ReleaseNotesOptions } from '../plugins/
|
|
4
|
+
import type { ReferenceItem, ReleaseNotesOptions } from '../plugins/replace/types.js';
|
|
5
5
|
import type { DoomSite } from '../shared/index.js';
|
|
6
6
|
export interface DoomConfig extends UserConfig {
|
|
7
7
|
api?: ApiPluginOptions;
|