@alauda/doom 0.6.6 → 0.6.8

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/new.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as prompts from '@inquirer/prompts';
2
2
  import { Command } from 'commander';
3
+ import { ContentProcessor } from '../utils/types.js';
3
4
  export declare const DEFAULT_PATH = "templates/scaffolding.yaml";
4
5
  export interface ScaffoldingTemplate {
5
6
  repo: string;
@@ -13,13 +14,9 @@ export interface ScaffoldingParameter<T extends keyof Prompts = keyof Prompts> {
13
14
  type: T;
14
15
  options: PromptOptions<T>;
15
16
  }
16
- export type ScaffoldingLayoutProcessor = {
17
- type: 'ejsTemplate';
18
- data?: Record<string, unknown>;
19
- };
20
17
  export type ScaffoldingLayoutWriteMode = 'append' | 'write';
21
18
  export interface ScaffoldingLayoutBase {
22
- processors?: ScaffoldingLayoutProcessor[];
19
+ processors?: ContentProcessor[];
23
20
  writeMode?: ScaffoldingLayoutWriteMode;
24
21
  }
25
22
  export interface ScaffoldingLayoutMatcher extends ScaffoldingLayoutBase {
@@ -1,6 +1,6 @@
1
1
  import type { RspressPlugin } from '@rspress/core';
2
2
  import type { ReferenceItem, ReleaseNotesOptions } from './types.js';
3
- export declare const referencePlugin: ({ base, root, lang, localBasePath, items, force, releaseNotes, }: {
3
+ export declare const referencePlugin: ({ root, lang, localBasePath, items, force, releaseNotes, }: {
4
4
  base: string;
5
5
  root: string;
6
6
  lang: string | null;
@@ -1,11 +1,9 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import { logger } from '@rspress/shared/logger';
3
- import remarkStringify from 'remark-stringify';
4
- import { remarkPluginNormalizeLink } from './remark-normalize-link.js';
5
- import { maybeHaveRef, remarkReplace } from './remark-replace.js';
3
+ import { MD_RELEASE_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN, refCache, releaseCache, remarkReplace, } from './remark-replace.js';
6
4
  import { remarkPluginToc } from './remark-toc.js';
7
5
  import { mdProcessor, mdxProcessor, normalizeReferenceItems } from './utils.js';
8
- export const referencePlugin = ({ base, root, lang, localBasePath, items = [], force, releaseNotes, }) => {
6
+ export const referencePlugin = ({ root, lang, localBasePath, items = [], force, releaseNotes, }) => {
9
7
  const normalizedItems = normalizeReferenceItems(items);
10
8
  return {
11
9
  name: 'doom-reference',
@@ -22,24 +20,26 @@ export const referencePlugin = ({ base, root, lang, localBasePath, items = [], f
22
20
  force,
23
21
  },
24
22
  ],
25
- remarkPluginToc,
26
- [remarkPluginNormalizeLink, { base, root }],
27
23
  ],
28
24
  },
29
- async modifySearchIndexData(pages) {
25
+ async modifySearchIndexData(pages, isProd) {
26
+ if (!isProd) {
27
+ refCache.clear();
28
+ releaseCache.clear();
29
+ }
30
30
  const results = await Promise.allSettled(pages.map(async (page) => {
31
31
  const filepath = page._filepath;
32
32
  if (!/\.mdx?$/.test(filepath)) {
33
33
  return;
34
34
  }
35
+ const isMdx = filepath.endsWith('.mdx');
35
36
  const content = await fs.readFile(filepath, 'utf8');
36
- if (!maybeHaveRef(filepath, content)) {
37
+ if (!(isMdx ? MDX_RELEASE_COMMENT_PATTERN : MD_RELEASE_COMMENT_PATTERN).test(content)) {
37
38
  return;
38
39
  }
39
- const processor = filepath.endsWith('.mdx')
40
- ? mdxProcessor
41
- : mdProcessor;
40
+ const processor = isMdx ? mdxProcessor : mdProcessor;
42
41
  const compiler = processor()
42
+ .data('pageMeta', {})
43
43
  .use(remarkReplace, {
44
44
  lang,
45
45
  localBasePath,
@@ -48,12 +48,13 @@ export const referencePlugin = ({ base, root, lang, localBasePath, items = [], f
48
48
  force,
49
49
  releaseNotes,
50
50
  })
51
- .use(remarkPluginToc)
52
- .use(remarkStringify);
53
- compiler.data('pageMeta', {});
51
+ .use(remarkPluginToc);
54
52
  const vfile = await compiler.process({
55
53
  path: filepath,
56
54
  value: content,
55
+ data: {
56
+ original: true,
57
+ },
57
58
  });
58
59
  const { toc, title } = compiler.data('pageMeta');
59
60
  page.title = title;
@@ -1,7 +1,11 @@
1
- import type { Root } from 'mdast';
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 declare const maybeHaveRef: (filepath: string, content: string) => boolean;
4
+ export declare const refCache: Map<string, Promise<Content[] | undefined>>;
5
+ export declare const releaseCache: Map<string, Promise<Record<string, Content | Content[]> | undefined>>;
6
+ export declare const MD_RELEASE_COMMENT_PATTERN: RegExp;
7
+ export declare const MDX_RELEASE_COMMENT_PATTERN: RegExp;
8
+ export declare const maybeHaveRef: (filepath: string, content: string) => boolean | undefined;
5
9
  export declare const remarkReplace: Plugin<[
6
10
  {
7
11
  lang: string | null;
@@ -4,11 +4,12 @@ import path from 'node:path';
4
4
  import { logger } from '@rspress/shared/logger';
5
5
  import { render } from 'ejs';
6
6
  import { simpleGit } from 'simple-git';
7
+ import { parse, stringify } from 'yaml';
7
8
  import { cyan, red } from 'yoctocolors';
8
9
  import { parseToc } from './remark-toc.js';
9
- import { flatMap, mdProcessor, mdxProcessor } from './utils.js';
10
+ import { getFrontmatterNode, mdProcessor, mdxProcessor } from './utils.js';
10
11
  const remotesFolder = path.resolve(os.homedir(), '.doom/remotes');
11
- const refCache = new Map();
12
+ export const refCache = new Map();
12
13
  const resolveReference_ = async (localBasePath, items, refName, force) => {
13
14
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
14
15
  if (!items[refName]) {
@@ -56,19 +57,35 @@ const resolveReference_ = async (localBasePath, items, refName, force) => {
56
57
  }
57
58
  if (!found) {
58
59
  logger.error(`Reference path \`${red(source.path)}\` for \`${red(source.name)}\` not found`);
60
+ return;
59
61
  }
60
- const processor = sourcePath.endsWith('.mdx') ? mdxProcessor : mdProcessor;
61
- const root = processor.parse(await fs.readFile(sourcePath));
62
+ let content = await fs.readFile(sourcePath, 'utf8');
63
+ for (const processor of source.processors ?? []) {
64
+ switch (processor.type) {
65
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
66
+ case 'ejsTemplate': {
67
+ content = await render(content, { data: processor.data }, { async: true });
68
+ break;
69
+ }
70
+ }
71
+ }
72
+ const processor = (sourcePath.endsWith('.ejs')
73
+ ? sourcePath.slice(0, -4)
74
+ : sourcePath).endsWith('.mdx')
75
+ ? mdxProcessor
76
+ : mdProcessor;
77
+ const root = processor.parse(content);
62
78
  if (!source.anchor) {
63
79
  return root.children;
64
80
  }
81
+ const frontmatterNode = getFrontmatterNode(root);
65
82
  const { toc } = parseToc(root, true);
66
83
  const active = toc.find((it) => it.id === source.anchor);
67
84
  if (!active) {
68
85
  logger.error(`Anchor \`${red(source.anchor)}\` not found in \`${red(source.name)}\``);
69
86
  return;
70
87
  }
71
- const nodes = [];
88
+ const nodes = frontmatterNode ? [frontmatterNode] : [];
72
89
  for (let i = active.index, n = root.children.length; i < n; i++) {
73
90
  if (i === active.index && (source.ignoreHeading ?? active.depth === 1)) {
74
91
  continue;
@@ -91,7 +108,7 @@ const resolveReference = async (localBasePath, items, refName, force) => {
91
108
  refCache.set(refName, resolving);
92
109
  return resolving;
93
110
  };
94
- const releaseCache = new Map();
111
+ export const releaseCache = new Map();
95
112
  const FIELD_MAPPER = {
96
113
  zh: 'customfield_13800',
97
114
  en: 'customfield_13801',
@@ -134,7 +151,7 @@ const resolveRelease_ = async (releaseTemplates, releaseQuery) => {
134
151
  const query = new URLSearchParams(releaseQuery);
135
152
  const templateName = query.get('template');
136
153
  if (!templateName) {
137
- logger.error(`Release notes template not found in query \`${red(releaseQuery)}\``);
154
+ logger.error(`Release notes template not found for query \`${red(releaseQuery)}\``);
138
155
  return;
139
156
  }
140
157
  const template = releaseTemplates[templateName];
@@ -181,46 +198,143 @@ const resolveRelease = async (releaseTemplates, releaseQuery, lang) => {
181
198
  const resolved = await resolving;
182
199
  return resolved?.[lang] ?? resolved?.en;
183
200
  };
184
- const MD_REF_COMMENT_PATTERN = /<!-{2,} *reference-include#(.+) *-{2,}>/;
185
- const MDX_REF_COMMENT_PATTERN = /{\/\*+ *reference-include#(.+) *\*+\/}/;
186
- const MDX_REF_PATTERN = /\/\*+ *reference-include#(.+) \*+\//;
187
- const MD_RELEASE_COMMENT_PATTERN = /<!-{2,} *release-notes-for-bugs\?(.+) *-{2,}>/;
188
- const MDX_RELEASE_COMMENT_PATTERN = /{\/\*+ *release-notes-for-bugs\?(.+) *\*+\/}/;
201
+ const MD_REF_START_COMMENT_PATTERN = /<!-{2,} *reference-start#(.+) *-{2,}>/;
202
+ const MDX_REF_START_COMMENT_PATTERN = /{\/\*+ *reference-start#(.+) *\*+\/}/;
203
+ const MDX_REF_START_PATTERN = /\/\*+ *reference-start#(.+) *\*+\//;
204
+ const MD_REF_END_COMMENT_PATTERN = /<!-{2,} *reference-end *-{2,}>/;
205
+ const MDX_REF_END_PATTERN = /\/\*+ *reference-end *\*+\//;
206
+ export const MD_RELEASE_COMMENT_PATTERN = /<!-{2,} *release-notes-for-bugs\?(.+) *-{2,}>/;
207
+ export const MDX_RELEASE_COMMENT_PATTERN = /{\/\*+ *release-notes-for-bugs\?(.+) *\*+\/}/;
189
208
  const MDX_RELEASE_PATTERN = /\/\*+ *release-notes-for-bugs\?(.+) *\*+\//;
190
- export const maybeHaveRef = (filepath, content) => (filepath.endsWith('.mdx')
191
- ? [MDX_REF_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN]
192
- : [MD_REF_COMMENT_PATTERN, MD_RELEASE_COMMENT_PATTERN]).some((p) => p.test(content));
209
+ export const maybeHaveRef = (filepath, content) => {
210
+ if (!/\.mdx?$/.test(filepath)) {
211
+ return;
212
+ }
213
+ return (filepath.endsWith('.mdx')
214
+ ? [MDX_REF_START_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN]
215
+ : [MD_REF_START_COMMENT_PATTERN, MD_RELEASE_COMMENT_PATTERN]).some((p) => p.test(content));
216
+ };
193
217
  export const remarkReplace = function ({ lang, localBasePath, root, items, force, releaseNotes }) {
194
218
  return async (ast, vfile) => {
195
219
  const filepath = vfile.path;
196
- const content = vfile.toString();
220
+ let content = vfile.toString();
197
221
  if (!maybeHaveRef(filepath, content)) {
198
222
  return;
199
223
  }
224
+ const isMdx = filepath.endsWith('.mdx');
225
+ const processor = isMdx ? mdxProcessor : mdProcessor;
226
+ const originalAst = vfile.data.original
227
+ ? ast
228
+ : processor.parse((content = await fs.readFile(filepath, 'utf8')));
229
+ const frontmatterNode = getFrontmatterNode(originalAst);
230
+ let frontmatter = frontmatterNode &&
231
+ parse(frontmatterNode.value);
200
232
  const relativePath = path.relative(root, filepath);
201
233
  const currLang = lang === null ? 'zh' : relativePath.split('/')[0];
202
- return flatMap(ast, async (node) => {
203
- let refMatched = null;
204
- let releaseMatched = null;
205
- if (node.type === 'html') {
206
- refMatched = node.value.match(MD_REF_COMMENT_PATTERN);
207
- releaseMatched = node.value.match(MD_RELEASE_COMMENT_PATTERN);
234
+ const newAstChildren = [];
235
+ const newContentChildren = [];
236
+ let index = 0;
237
+ let start = null;
238
+ let refName = '';
239
+ let matched;
240
+ let checkContent = false;
241
+ for (const node of ast.children) {
242
+ index++;
243
+ if (node.type !== (isMdx ? 'mdxFlowExpression' : 'html')) {
244
+ if (node.type === 'yaml') {
245
+ continue;
246
+ }
247
+ newAstChildren.push(node);
248
+ if (start == null) {
249
+ newContentChildren.push(node);
250
+ }
251
+ continue;
208
252
  }
209
- else if (node.type === 'mdxFlowExpression') {
210
- refMatched = node.value.match(MDX_REF_PATTERN);
211
- releaseMatched = node.value.match(MDX_RELEASE_PATTERN);
253
+ if ((matched = node.value.match(isMdx ? MDX_REF_START_PATTERN : MD_REF_START_COMMENT_PATTERN))) {
254
+ if (start != null) {
255
+ logger.warn(`Invalid reference start block ${red(node.value)}, nested reference blocks are not allowed`);
256
+ }
257
+ else {
258
+ checkContent = true;
259
+ start = index;
260
+ refName = matched[1].trim();
261
+ }
262
+ newAstChildren.push(node);
263
+ newContentChildren.push(node);
264
+ continue;
212
265
  }
213
- else {
214
- return;
266
+ if ((isMdx ? MDX_REF_END_PATTERN : MD_REF_END_COMMENT_PATTERN).test(node.value)) {
267
+ if (start == null) {
268
+ logger.warn(`Invalid reference end block ${red(node.value)}, no matching start block found`);
269
+ }
270
+ else {
271
+ start = null;
272
+ }
273
+ const resolved = await resolveReference(localBasePath, items, refName, force);
274
+ if (resolved) {
275
+ const { frontmatterMode } = items[refName];
276
+ for (const nodeItem of resolved) {
277
+ if (nodeItem.type !== 'yaml') {
278
+ newContentChildren.push(nodeItem);
279
+ continue;
280
+ }
281
+ if (!frontmatterMode || frontmatterMode === 'ignore') {
282
+ continue;
283
+ }
284
+ if (frontmatterMode === 'remove') {
285
+ frontmatter = null;
286
+ continue;
287
+ }
288
+ const data = parse(nodeItem.value);
289
+ if (frontmatterMode === 'merge') {
290
+ if (data) {
291
+ frontmatter = {
292
+ ...frontmatter,
293
+ ...data,
294
+ };
295
+ }
296
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
297
+ }
298
+ else if (frontmatterMode === 'replace') {
299
+ frontmatter = data;
300
+ }
301
+ }
302
+ }
303
+ newAstChildren.push(node);
304
+ newContentChildren.push(node);
305
+ continue;
215
306
  }
216
- const refName = refMatched?.[1];
217
- const releaseQuery = releaseMatched?.[1];
218
- if (refName) {
219
- return resolveReference(localBasePath, items, refName.trim(), force);
307
+ if ((matched = node.value.match(isMdx ? MDX_RELEASE_PATTERN : MD_RELEASE_COMMENT_PATTERN))) {
308
+ const releaseContent = await resolveRelease(releaseNotes?.queryTemplates ?? {}, matched[1].trim(), currLang);
309
+ newAstChildren.push(releaseContent ?? node);
310
+ newContentChildren.push(node);
220
311
  }
221
- if (releaseNotes?.queryTemplates && releaseQuery) {
222
- return resolveRelease(releaseNotes.queryTemplates, releaseQuery.trim(), currLang);
312
+ }
313
+ if (start != null) {
314
+ logger.warn(`Invalid reference start block ${red(refName)}, no matching end block found, adding for you`);
315
+ newContentChildren.push({
316
+ type: isMdx ? 'mdxFlowExpression' : 'html',
317
+ value: isMdx ? '/* reference-end */' : '<!-- reference-end -->',
318
+ });
319
+ }
320
+ if (frontmatter) {
321
+ newContentChildren.unshift({
322
+ type: 'yaml',
323
+ value: stringify(frontmatter).trim(),
324
+ });
325
+ }
326
+ if (checkContent) {
327
+ const newContent = processor.stringify({
328
+ ...ast,
329
+ children: newContentChildren
330
+ .flat()
331
+ .filter((n) => n.type !== 'mdxjsEsm'),
332
+ });
333
+ if (content !== newContent) {
334
+ await fs.writeFile(filepath, newContent);
335
+ return;
223
336
  }
224
- });
337
+ }
338
+ ast.children = newAstChildren.flat();
225
339
  };
226
340
  };
@@ -1,7 +1,13 @@
1
+ import { ContentProcessor } from '../../utils/types.js';
1
2
  export interface ReferenceItemSource {
2
3
  name: string;
3
4
  path: string;
4
5
  ignoreHeading?: boolean;
6
+ /**
7
+ * @default 'ignore'
8
+ */
9
+ frontmatterMode?: 'ignore' | 'merge' | 'remove' | 'replace';
10
+ processors?: ContentProcessor[];
5
11
  }
6
12
  export interface ReferenceItem {
7
13
  repo?: string;
@@ -1,13 +1,14 @@
1
- import type { Parent, Root } from 'mdast';
1
+ import type { Root, YAML } from 'mdast';
2
2
  import type { MdxjsEsm } from 'mdast-util-mdxjs-esm';
3
- import type { Node } from 'unist';
3
+ import { type Options } from 'remark-stringify';
4
4
  import type { NormalizedReferenceSource, ReferenceItem } from './types.js';
5
5
  /**
6
6
  * hello world {#custom-id} -> { text: 'hello world', id: 'custom-id' }
7
7
  */
8
8
  export declare const extractTextAndId: (title: string) => string[];
9
9
  export declare const normalizeReferenceItems: (items: ReferenceItem[]) => Record<string, NormalizedReferenceSource>;
10
- export declare const flatMap: (root: Root, fn: (node: Node, index: number, parent: Parent | null) => Promise<Node | Node[] | undefined> | Node | Node[] | undefined) => Promise<Root>;
11
10
  export declare const getASTNodeImport: (name: string, from: string) => MdxjsEsm;
12
- export declare const mdProcessor: import("unified").FrozenProcessor<Root, Root, Root, void>;
13
- export declare const mdxProcessor: import("unified").FrozenProcessor<Root, Root, Root, void>;
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;
@@ -3,6 +3,7 @@ import remarkFrontmatter from 'remark-frontmatter';
3
3
  import remarkGfm from 'remark-gfm';
4
4
  import remarkMdx from 'remark-mdx';
5
5
  import remarkParse from 'remark-parse';
6
+ import remarkStringify from 'remark-stringify';
6
7
  import { unified } from 'unified';
7
8
  import { red } from 'yoctocolors';
8
9
  /**
@@ -36,33 +37,6 @@ export const normalizeReferenceItems = (items) => items.reduce((acc, curr) => {
36
37
  }
37
38
  return acc;
38
39
  }, {});
39
- export const flatMap = async (root, fn) => {
40
- const transformed = await transform(root, 0, null);
41
- if (!transformed) {
42
- return root;
43
- }
44
- return (Array.isArray(transformed) ? transformed[0] : transformed);
45
- async function transform(node, index, parent) {
46
- if ('children' in node && node.children.length) {
47
- const out = [];
48
- for (let i = 0, n = node.children.length; i < n; i++) {
49
- const child = node.children[i];
50
- let xs = await transform(child, i, node);
51
- if (xs) {
52
- xs = Array.isArray(xs) ? xs : [xs];
53
- for (let j = 0, m = xs.length; j < m; j++) {
54
- out.push(xs[j]);
55
- }
56
- }
57
- else {
58
- out.push(child);
59
- }
60
- }
61
- node.children = out;
62
- }
63
- return fn(node, index, parent);
64
- }
65
- };
66
40
  // Construct import statement for AST
67
41
  // Such as: import image1 from './test.png'
68
42
  export const getASTNodeImport = (name, from) => ({
@@ -91,9 +65,18 @@ export const getASTNodeImport = (name, from) => ({
91
65
  },
92
66
  },
93
67
  });
68
+ export const stringifySettings = {
69
+ bullet: '-',
70
+ listItemIndent: 'one',
71
+ };
94
72
  export const mdProcessor = unified()
95
73
  .use(remarkParse)
96
74
  .use(remarkFrontmatter)
97
75
  .use(remarkGfm)
76
+ .use(remarkStringify, stringifySettings)
98
77
  .freeze();
99
78
  export const mdxProcessor = mdProcessor().use(remarkMdx).freeze();
79
+ export const getFrontmatterNode = (ast) => {
80
+ const firstNode = ast.children[0];
81
+ return firstNode.type === 'yaml' ? firstNode : undefined;
82
+ };
@@ -10,3 +10,7 @@ export interface DoomConfig extends UserConfig {
10
10
  sidebar?: AutoSidebarPluginOptions;
11
11
  releaseNotes?: ReleaseNotesOptions;
12
12
  }
13
+ export type ContentProcessor = {
14
+ type: 'ejsTemplate';
15
+ data?: Record<string, unknown>;
16
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.6.6",
3
+ "version": "0.6.8",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {