@alauda/doom 0.6.6 → 0.6.7

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;
@@ -3,12 +3,15 @@ import os from 'node:os';
3
3
  import path from 'node:path';
4
4
  import { logger } from '@rspress/shared/logger';
5
5
  import { render } from 'ejs';
6
+ import remarkFrontmatter from 'remark-frontmatter';
7
+ import remarkStringify from 'remark-stringify';
6
8
  import { simpleGit } from 'simple-git';
9
+ import { parse, stringify } from 'yaml';
7
10
  import { cyan, red } from 'yoctocolors';
8
11
  import { parseToc } from './remark-toc.js';
9
- import { flatMap, mdProcessor, mdxProcessor } from './utils.js';
12
+ import { getFrontmatterNode, mdProcessor, mdxProcessor, stringifySettings, } from './utils.js';
10
13
  const remotesFolder = path.resolve(os.homedir(), '.doom/remotes');
11
- const refCache = new Map();
14
+ export const refCache = new Map();
12
15
  const resolveReference_ = async (localBasePath, items, refName, force) => {
13
16
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
14
17
  if (!items[refName]) {
@@ -56,19 +59,35 @@ const resolveReference_ = async (localBasePath, items, refName, force) => {
56
59
  }
57
60
  if (!found) {
58
61
  logger.error(`Reference path \`${red(source.path)}\` for \`${red(source.name)}\` not found`);
62
+ return;
59
63
  }
60
- const processor = sourcePath.endsWith('.mdx') ? mdxProcessor : mdProcessor;
61
- const root = processor.parse(await fs.readFile(sourcePath));
64
+ let content = await fs.readFile(sourcePath, 'utf8');
65
+ for (const processor of source.processors ?? []) {
66
+ switch (processor.type) {
67
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
68
+ case 'ejsTemplate': {
69
+ content = await render(content, { data: processor.data }, { async: true });
70
+ break;
71
+ }
72
+ }
73
+ }
74
+ const processor = (sourcePath.endsWith('.ejs')
75
+ ? sourcePath.slice(0, -4)
76
+ : sourcePath).endsWith('.mdx')
77
+ ? mdxProcessor
78
+ : mdProcessor;
79
+ const root = processor.parse(content);
62
80
  if (!source.anchor) {
63
81
  return root.children;
64
82
  }
83
+ const frontmatterNode = getFrontmatterNode(root);
65
84
  const { toc } = parseToc(root, true);
66
85
  const active = toc.find((it) => it.id === source.anchor);
67
86
  if (!active) {
68
87
  logger.error(`Anchor \`${red(source.anchor)}\` not found in \`${red(source.name)}\``);
69
88
  return;
70
89
  }
71
- const nodes = [];
90
+ const nodes = frontmatterNode ? [frontmatterNode] : [];
72
91
  for (let i = active.index, n = root.children.length; i < n; i++) {
73
92
  if (i === active.index && (source.ignoreHeading ?? active.depth === 1)) {
74
93
  continue;
@@ -91,7 +110,7 @@ const resolveReference = async (localBasePath, items, refName, force) => {
91
110
  refCache.set(refName, resolving);
92
111
  return resolving;
93
112
  };
94
- const releaseCache = new Map();
113
+ export const releaseCache = new Map();
95
114
  const FIELD_MAPPER = {
96
115
  zh: 'customfield_13800',
97
116
  en: 'customfield_13801',
@@ -134,7 +153,7 @@ const resolveRelease_ = async (releaseTemplates, releaseQuery) => {
134
153
  const query = new URLSearchParams(releaseQuery);
135
154
  const templateName = query.get('template');
136
155
  if (!templateName) {
137
- logger.error(`Release notes template not found in query \`${red(releaseQuery)}\``);
156
+ logger.error(`Release notes template not found for query \`${red(releaseQuery)}\``);
138
157
  return;
139
158
  }
140
159
  const template = releaseTemplates[templateName];
@@ -181,46 +200,141 @@ const resolveRelease = async (releaseTemplates, releaseQuery, lang) => {
181
200
  const resolved = await resolving;
182
201
  return resolved?.[lang] ?? resolved?.en;
183
202
  };
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\?(.+) *\*+\/}/;
203
+ const MD_REF_START_COMMENT_PATTERN = /<!-{2,} *reference-start#(.+) *-{2,}>/;
204
+ const MDX_REF_START_COMMENT_PATTERN = /{\/\*+ *reference-start#(.+) *\*+\/}/;
205
+ const MDX_REF_START_PATTERN = /\/\*+ *reference-start#(.+) *\*+\//;
206
+ const MD_REF_END_COMMENT_PATTERN = /<!-{2,} *reference-end *-{2,}>/;
207
+ const MDX_REF_END_PATTERN = /\/\*+ *reference-end *\*+\//;
208
+ export const MD_RELEASE_COMMENT_PATTERN = /<!-{2,} *release-notes-for-bugs\?(.+) *-{2,}>/;
209
+ export const MDX_RELEASE_COMMENT_PATTERN = /{\/\*+ *release-notes-for-bugs\?(.+) *\*+\/}/;
189
210
  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));
211
+ export const maybeHaveRef = (filepath, content) => {
212
+ if (!/\.mdx?$/.test(filepath)) {
213
+ return;
214
+ }
215
+ return (filepath.endsWith('.mdx')
216
+ ? [MDX_REF_START_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN]
217
+ : [MD_REF_START_COMMENT_PATTERN, MD_RELEASE_COMMENT_PATTERN]).some((p) => p.test(content));
218
+ };
193
219
  export const remarkReplace = function ({ lang, localBasePath, root, items, force, releaseNotes }) {
194
220
  return async (ast, vfile) => {
195
221
  const filepath = vfile.path;
196
- const content = vfile.toString();
222
+ let content = vfile.toString();
197
223
  if (!maybeHaveRef(filepath, content)) {
198
224
  return;
199
225
  }
226
+ const processor = this()
227
+ .use(remarkFrontmatter)
228
+ .use(remarkStringify, stringifySettings);
229
+ const originalAst = vfile.data.original
230
+ ? ast
231
+ : processor.parse((content = await fs.readFile(filepath, 'utf8')));
232
+ const frontmatterNode = getFrontmatterNode(originalAst);
233
+ let frontmatter = frontmatterNode &&
234
+ parse(frontmatterNode.value);
235
+ const isMdx = filepath.endsWith('.mdx');
200
236
  const relativePath = path.relative(root, filepath);
201
237
  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);
238
+ const references = [];
239
+ const newAstChildren = [];
240
+ const newContentChildren = [];
241
+ let index = 0;
242
+ let start = null;
243
+ let refName = '';
244
+ let matched;
245
+ for (const node of ast.children) {
246
+ index++;
247
+ if (node.type !== (isMdx ? 'mdxFlowExpression' : 'html')) {
248
+ if (node.type === 'yaml') {
249
+ continue;
250
+ }
251
+ newAstChildren.push(node);
252
+ if (start == null) {
253
+ newContentChildren.push(node);
254
+ }
255
+ continue;
208
256
  }
209
- else if (node.type === 'mdxFlowExpression') {
210
- refMatched = node.value.match(MDX_REF_PATTERN);
211
- releaseMatched = node.value.match(MDX_RELEASE_PATTERN);
257
+ if ((matched = node.value.match(isMdx ? MDX_REF_START_PATTERN : MD_REF_START_COMMENT_PATTERN))) {
258
+ if (start != null) {
259
+ logger.warn(`Invalid reference start block ${node.value}, nested reference blocks are not allowed`);
260
+ }
261
+ else {
262
+ start = index;
263
+ refName = matched[1].trim();
264
+ }
265
+ newAstChildren.push(node);
266
+ newContentChildren.push(node);
267
+ continue;
212
268
  }
213
- else {
214
- return;
215
- }
216
- const refName = refMatched?.[1];
217
- const releaseQuery = releaseMatched?.[1];
218
- if (refName) {
219
- return resolveReference(localBasePath, items, refName.trim(), force);
269
+ if ((isMdx ? MDX_REF_END_PATTERN : MD_REF_END_COMMENT_PATTERN).test(node.value)) {
270
+ if (start == null) {
271
+ logger.warn(`Invalid reference end block ${node.value}, no matching start block found`);
272
+ }
273
+ else {
274
+ references.push([start, index]);
275
+ start = null;
276
+ }
277
+ const resolved = await resolveReference(localBasePath, items, refName, force);
278
+ if (resolved) {
279
+ const { frontmatterMode } = items[refName];
280
+ for (const nodeItem of resolved) {
281
+ if (nodeItem.type !== 'yaml') {
282
+ newContentChildren.push(nodeItem);
283
+ continue;
284
+ }
285
+ if (!frontmatterMode || frontmatterMode === 'ignore') {
286
+ continue;
287
+ }
288
+ if (frontmatterMode === 'remove') {
289
+ frontmatter = null;
290
+ continue;
291
+ }
292
+ const data = parse(nodeItem.value);
293
+ if (frontmatterMode === 'merge') {
294
+ if (data) {
295
+ frontmatter = {
296
+ ...frontmatter,
297
+ ...data,
298
+ };
299
+ }
300
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
301
+ }
302
+ else if (frontmatterMode === 'replace') {
303
+ frontmatter = data;
304
+ }
305
+ }
306
+ }
307
+ newAstChildren.push(node);
308
+ newContentChildren.push(node);
309
+ continue;
220
310
  }
221
- if (releaseNotes?.queryTemplates && releaseQuery) {
222
- return resolveRelease(releaseNotes.queryTemplates, releaseQuery.trim(), currLang);
311
+ if ((matched = node.value.match(isMdx ? MDX_RELEASE_PATTERN : MD_RELEASE_COMMENT_PATTERN))) {
312
+ const releaseContent = await resolveRelease(releaseNotes?.queryTemplates ?? {}, matched[1].trim(), currLang);
313
+ newAstChildren.push(releaseContent ?? node);
314
+ newContentChildren.push(node);
223
315
  }
316
+ }
317
+ if (start != null) {
318
+ logger.warn(`Invalid reference start block ${start}, no matching end block found, adding for you`);
319
+ newContentChildren.push({
320
+ type: isMdx ? 'mdxFlowExpression' : 'html',
321
+ value: isMdx ? '/* reference-end */' : '<!-- reference-end -->',
322
+ });
323
+ }
324
+ if (frontmatter) {
325
+ newContentChildren.unshift({
326
+ type: 'yaml',
327
+ value: stringify(frontmatter).trim(),
328
+ });
329
+ }
330
+ const newContent = processor.stringify({
331
+ ...ast,
332
+ children: newContentChildren.flat().filter((n) => n.type !== 'mdxjsEsm'),
224
333
  });
334
+ if (content !== newContent) {
335
+ await fs.writeFile(filepath, newContent);
336
+ return;
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.7",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {