@alauda/doom 0.6.11 → 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/cli/new.js CHANGED
@@ -79,7 +79,7 @@ const resolveScaffoldings = async (name, force) => {
79
79
  for (const branch of branches) {
80
80
  if (branch === currentBranch) {
81
81
  logger.info(`Pulling latest changes from branch \`${cyan(branch)}\`...`);
82
- await git.pull([...options, '--rebase']);
82
+ await git.pull([...options, '--rebase', '--allow-unrelated-histories']);
83
83
  }
84
84
  else {
85
85
  logger.info(`Switching to branch \`${cyan(branch)}\`...`);
@@ -169,7 +169,9 @@ export const newCommand = new Command('new')
169
169
  });
170
170
  const files = new Set(dirents
171
171
  .filter((d) => d.isFile())
172
- .map((d) => path.relative(source, path.resolve(d.parentPath, d.name))));
172
+ .map((d) => path.relative(source, path.resolve(d.parentPath ||
173
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
174
+ d.path, d.name))));
173
175
  for (const matcher of layout.matchers || []) {
174
176
  const matched = await glob(matcher.match.map((m) => render(m, { parameters })), { cwd: source });
175
177
  for (const file of matched) {
@@ -1,7 +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, refCache, releaseCache, remarkReplace, } from './remark-replace.js';
4
- import { remarkPluginToc } from './remark-toc.js';
3
+ import { MD_RELEASE_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN, remarkReplace, } from './remark-replace.js';
5
4
  import { mdProcessor, mdxProcessor, normalizeReferenceItems } from './utils.js';
6
5
  export const referencePlugin = ({ root, lang, localBasePath, items = [], force, releaseNotes, }) => {
7
6
  const normalizedItems = normalizeReferenceItems(items);
@@ -22,11 +21,7 @@ export const referencePlugin = ({ root, lang, localBasePath, items = [], force,
22
21
  ],
23
22
  ],
24
23
  },
25
- async modifySearchIndexData(pages, isProd) {
26
- if (!isProd) {
27
- refCache.clear();
28
- releaseCache.clear();
29
- }
24
+ async modifySearchIndexData(pages) {
30
25
  const results = await Promise.allSettled(pages.map(async (page) => {
31
26
  const filepath = page._filepath;
32
27
  if (!/\.mdx?$/.test(filepath)) {
@@ -38,17 +33,14 @@ export const referencePlugin = ({ root, lang, localBasePath, items = [], force,
38
33
  return;
39
34
  }
40
35
  const processor = isMdx ? mdxProcessor : mdProcessor;
41
- const compiler = processor()
42
- .data('pageMeta', {})
43
- .use(remarkReplace, {
36
+ const compiler = processor().data('pageMeta', {}).use(remarkReplace, {
44
37
  lang,
45
38
  localBasePath,
46
39
  root,
47
40
  items: normalizedItems,
48
41
  force,
49
42
  releaseNotes,
50
- })
51
- .use(remarkPluginToc);
43
+ });
52
44
  const vfile = await compiler.process({
53
45
  path: filepath,
54
46
  value: content,
@@ -56,9 +48,6 @@ export const referencePlugin = ({ root, lang, localBasePath, items = [], force,
56
48
  original: true,
57
49
  },
58
50
  });
59
- const { toc, title } = compiler.data('pageMeta');
60
- page.title = title;
61
- page.toc = toc;
62
51
  page.content = vfile.toString();
63
52
  }));
64
53
  for (const result of results) {
@@ -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
+ };
@@ -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 declare const refCache: Map<string, Promise<Content[] | undefined>>;
5
- export declare const releaseCache: Map<string, Promise<Record<string, Content | Content[]> | undefined>>;
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 { parseToc } from './remark-toc.js';
10
+ import { normalizeImgSrc } from './normalize-img-src.js';
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
- export const refCache = new Map();
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);
@@ -41,11 +43,19 @@ const resolveReference_ = async (localBasePath, items, refName, force) => {
41
43
  }
42
44
  if (force) {
43
45
  logger.info(`Pulling latest changes for \`${cyan(source.slug)}\`...`);
44
- await git.pull(['--depth', '1', '--force', '--rebase']);
46
+ await git.pull([
47
+ '--depth',
48
+ '1',
49
+ '--force',
50
+ '--rebase',
51
+ '--allow-unrelated-histories',
52
+ ]);
45
53
  }
54
+ publicBase = path.resolve(repoFolder, source.publicBase ?? 'docs/public');
46
55
  sourcePath = path.resolve(repoFolder, sourcePath);
47
56
  }
48
57
  else {
58
+ publicBase = localPublicBase;
49
59
  sourcePath = path.resolve(localBasePath, sourcePath);
50
60
  }
51
61
  let found = false;
@@ -76,8 +86,13 @@ const resolveReference_ = async (localBasePath, items, refName, force) => {
76
86
  ? mdxProcessor
77
87
  : mdProcessor;
78
88
  const root = processor.parse(content);
89
+ const sourceBase = path.dirname(sourcePath);
79
90
  if (!source.anchor) {
80
- return root.children;
91
+ return {
92
+ publicBase,
93
+ sourceBase,
94
+ contents: root.children,
95
+ };
81
96
  }
82
97
  const frontmatterNode = getFrontmatterNode(root);
83
98
  const { toc } = parseToc(root, true);
@@ -99,17 +114,26 @@ const resolveReference_ = async (localBasePath, items, refName, force) => {
99
114
  }
100
115
  nodes.push(node);
101
116
  }
102
- return nodes;
117
+ return {
118
+ publicBase,
119
+ sourceBase,
120
+ contents: nodes,
121
+ };
103
122
  };
104
- const resolveReference = async (localBasePath, items, refName, force) => {
123
+ const resolveReference = async (localBasePath, localPublicBase, items, refName, force) => {
105
124
  if (refCache.has(refName)) {
106
125
  return refCache.get(refName);
107
126
  }
108
- const resolving = resolveReference_(localBasePath, items, refName, force);
127
+ const resolving = resolveReference_(localBasePath, localPublicBase, items, refName, force);
109
128
  refCache.set(refName, resolving);
129
+ if (!isProduction()) {
130
+ setTimeout(() => {
131
+ refCache.delete(refName);
132
+ }, 5_000);
133
+ }
110
134
  return resolving;
111
135
  };
112
- export const releaseCache = new Map();
136
+ const releaseCache = new Map();
113
137
  const FIELD_MAPPER = {
114
138
  zh: 'customfield_13800',
115
139
  en: 'customfield_13801',
@@ -223,6 +247,8 @@ export const remarkReplace = function ({ lang, localBasePath, root, items, force
223
247
  if (!maybeHaveRef(filepath, content)) {
224
248
  return;
225
249
  }
250
+ const localPublicBase = path.resolve(root, 'public');
251
+ const targetBase = path.dirname(filepath);
226
252
  const isMdx = filepath.endsWith('.mdx');
227
253
  const processor = isMdx ? mdxProcessor : mdProcessor;
228
254
  const originalAst = vfile.data.original
@@ -272,12 +298,20 @@ export const remarkReplace = function ({ lang, localBasePath, root, items, force
272
298
  else {
273
299
  start = null;
274
300
  }
275
- const resolved = await resolveReference(localBasePath, items, refName, force);
301
+ const resolved = await resolveReference(localBasePath, localPublicBase, items, refName, force);
276
302
  if (resolved) {
277
- const { frontmatterMode } = items[refName];
278
- for (const nodeItem of resolved) {
303
+ const refSource = items[refName];
304
+ const { frontmatterMode } = refSource;
305
+ for (const nodeItem of resolved.contents) {
279
306
  if (nodeItem.type !== 'yaml') {
280
- 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
+ }));
281
315
  continue;
282
316
  }
283
317
  if (!frontmatterMode || frontmatterMode === 'ignore') {
@@ -316,6 +350,11 @@ export const remarkReplace = function ({ lang, localBasePath, root, items, force
316
350
  newAstChildren.push(releaseContent ?? node);
317
351
  }
318
352
  newContentChildren.push(node);
353
+ continue;
354
+ }
355
+ newAstChildren.push(node);
356
+ if (start == null) {
357
+ newContentChildren.push(node);
319
358
  }
320
359
  }
321
360
  if (start != null) {
@@ -332,15 +371,14 @@ export const remarkReplace = function ({ lang, localBasePath, root, items, force
332
371
  });
333
372
  }
334
373
  if (checkContent) {
374
+ const originalImports = originalAst.children
375
+ .filter((it) => it.type === 'mdxjsEsm')
376
+ .map((it) => it.value);
335
377
  const newContent = processor.stringify({
336
378
  ...ast,
337
- children: newContentChildren.flat().filter((n) => {
338
- if (n.type !== 'mdxjsEsm') {
339
- return true;
340
- }
341
- const matched = n.value.match(/^import\b.+\bfrom\b\s+(['"])([^'"]+)\1$/);
342
- return !matched?.[2] || !path.isAbsolute(matched[2]);
343
- }),
379
+ children: newContentChildren
380
+ .flat()
381
+ .filter((n) => n.type !== 'mdxjsEsm' || originalImports.includes(n.value)),
344
382
  });
345
383
  if (content !== newContent) {
346
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
  }
@@ -31,6 +31,7 @@ export const normalizeReferenceItems = (items) => items.reduce((acc, curr) => {
31
31
  acc[source.name] = {
32
32
  ...source,
33
33
  ...extra,
34
+ publicBase: curr.publicBase,
34
35
  path,
35
36
  anchor,
36
37
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.6.11",
3
+ "version": "0.6.13",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "packageManager": "yarn@4.5.0",
11
11
  "engines": {
12
- "node": ">=18.20.0 <20 || >=20.1.0"
12
+ "node": ">=18.17.0 <20 || >=20.1.0"
13
13
  },
14
14
  "bin": "lib/cli/index.js",
15
15
  "exports": {