@alauda/doom 0.15.2 → 0.15.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.
@@ -1,5 +1,6 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import { logger } from '@rspress/shared/logger';
3
+ import { rehypeNormalizeLink } from './rehype-normalize-link.js';
3
4
  import { remarkExplicitJsx } from './remark-explicit-jsx.js';
4
5
  import { MD_RELEASE_COMMENT_PATTERN, MDX_RELEASE_COMMENT_PATTERN, remarkReplace, } from './remark-replace.js';
5
6
  import { mdProcessor, mdxProcessor, normalizeReferenceItems } from './utils.js';
@@ -11,8 +12,8 @@ export const replacePlugin = ({ lang, localBasePath, force, }) => {
11
12
  return {
12
13
  name: 'doom-replace',
13
14
  config(config) {
14
- config.markdown = config.markdown ?? {};
15
- config.markdown.remarkPlugins = config.markdown.remarkPlugins ?? [];
15
+ config.markdown ??= {};
16
+ config.markdown.remarkPlugins ??= [];
16
17
  config.markdown.remarkPlugins.push([
17
18
  remarkReplace,
18
19
  {
@@ -24,6 +25,8 @@ export const replacePlugin = ({ lang, localBasePath, force, }) => {
24
25
  force,
25
26
  },
26
27
  ], remarkExplicitJsx);
28
+ config.markdown.rehypePlugins ??= [];
29
+ config.markdown.rehypePlugins.push(rehypeNormalizeLink);
27
30
  return (userConfig = config);
28
31
  },
29
32
  async modifySearchIndexData(pages) {
@@ -3,7 +3,7 @@ import path from 'node:path';
3
3
  import { isExternalUrl, removeLeadingSlash } from '@rspress/core';
4
4
  import { cloneDeep } from 'es-toolkit';
5
5
  import { visit } from 'unist-util-visit';
6
- const RELATIVE_URL_PATTERN = /^\.\.?\//;
6
+ import { RELATIVE_URL_PATTERN } from './utils.js';
7
7
  export const normalizeImgSrc = (content, { refSource, localPublicBase, publicBase, sourceBase, targetBase, force, translating, }) => {
8
8
  content = cloneDeep(content);
9
9
  const resolveImgSrc = (src, force) => {
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'unified';
2
+ import type { Root } from 'hast';
3
+ export declare const rehypeNormalizeLink: Plugin<[], Root>;
@@ -0,0 +1,69 @@
1
+ import { visit } from 'unist-util-visit';
2
+ import { isExternalUrl } from '@rspress/core';
3
+ import { getASTNodeImport } from './utils.js';
4
+ const getMdxSrcAttribute = (tempVar) => {
5
+ return {
6
+ type: 'mdxJsxAttribute',
7
+ name: 'src',
8
+ value: {
9
+ type: 'mdxJsxAttributeValueExpression',
10
+ value: tempVar,
11
+ data: {
12
+ estree: {
13
+ type: 'Program',
14
+ sourceType: 'module',
15
+ body: [
16
+ {
17
+ type: 'ExpressionStatement',
18
+ expression: {
19
+ type: 'Identifier',
20
+ name: tempVar,
21
+ },
22
+ },
23
+ ],
24
+ },
25
+ },
26
+ },
27
+ };
28
+ };
29
+ const normalizeImageUrl = (imageUrl) => {
30
+ if (isExternalUrl(imageUrl) || imageUrl.startsWith('/')) {
31
+ return;
32
+ }
33
+ return imageUrl;
34
+ };
35
+ export const rehypeNormalizeLink = () => {
36
+ return (tree) => {
37
+ const images = [];
38
+ visit(tree, 'element', (node) => {
39
+ if (node.tagName !== 'img') {
40
+ return;
41
+ }
42
+ const { alt, src } = node.properties || {};
43
+ if (typeof src !== 'string') {
44
+ return;
45
+ }
46
+ const imagePath = normalizeImageUrl(src);
47
+ if (!imagePath) {
48
+ return;
49
+ }
50
+ // relative path
51
+ const tempVariableName = `doom_image${images.length}`;
52
+ Object.assign(node, {
53
+ type: 'mdxJsxFlowElement',
54
+ name: 'img',
55
+ children: [],
56
+ attributes: [
57
+ alt && {
58
+ type: 'mdxJsxAttribute',
59
+ name: 'alt',
60
+ value: alt,
61
+ },
62
+ getMdxSrcAttribute(tempVariableName),
63
+ ].filter(Boolean),
64
+ });
65
+ images.push(getASTNodeImport(tempVariableName, imagePath));
66
+ });
67
+ tree.children.unshift(...images);
68
+ };
69
+ };
@@ -2,6 +2,7 @@ import type { Root, YAML } from 'mdast';
2
2
  import type { MdxjsEsm } from 'mdast-util-mdxjs-esm';
3
3
  import { type Options } from 'remark-stringify';
4
4
  import type { NormalizedReferenceSource, ReferenceItem } from './types.js';
5
+ export declare const RELATIVE_URL_PATTERN: RegExp;
5
6
  /**
6
7
  * hello world {#custom-id} -> { text: 'hello world', id: 'custom-id' }
7
8
  */
@@ -6,6 +6,7 @@ import remarkParse from 'remark-parse';
6
6
  import remarkStringify, {} from 'remark-stringify';
7
7
  import { unified } from 'unified';
8
8
  import { red } from 'yoctocolors';
9
+ export const RELATIVE_URL_PATTERN = /^\.\.?\//;
9
10
  /**
10
11
  * hello world {#custom-id} -> { text: 'hello world', id: 'custom-id' }
11
12
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.15.2",
3
+ "version": "0.15.3",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {