@alauda/doom 2.5.2 → 2.5.4

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/index.js CHANGED
@@ -85,7 +85,7 @@ program
85
85
  cwd: CWD,
86
86
  });
87
87
  let isRestarting = false;
88
- cliWatcher.on('all', async (eventName, filepath) => {
88
+ cliWatcher.on('all', async (...[eventName, filepath]) => {
89
89
  console.log(eventName, filepath);
90
90
  if (eventName === 'add' ||
91
91
  eventName === 'unlink' ||
package/lib/eslint.js CHANGED
@@ -1,4 +1,6 @@
1
+ import fs from 'node:fs';
1
2
  import { createRequire } from 'node:module';
3
+ import path from 'node:path';
2
4
  import { fileURLToPath } from 'node:url';
3
5
  import cspellRecommended from '@cspell/eslint-plugin/recommended';
4
6
  import js from '@eslint/js';
@@ -9,14 +11,33 @@ import * as mdx from 'eslint-plugin-mdx';
9
11
  import globals from 'globals';
10
12
  import tseslint from 'typescript-eslint';
11
13
  import { loadConfig } from './cli/load-config.js';
14
+ import { getGlobalComponentNames } from './plugins/global/components.js';
15
+ import { pkgResolve } from './utils/index.js';
12
16
  const cjsRequire = createRequire(import.meta.url);
13
17
  let remarkConfigPath;
18
+ const isLoadableRemarkConfigPath = (filepath) => ['.cjs', '.js', '.mjs'].includes(path.extname(filepath));
14
19
  try {
15
- remarkConfigPath = fileURLToPath(import.meta.resolve('./remarkrc.js'));
20
+ const resolvedRemarkConfigPath = fileURLToPath(import.meta.resolve('./remarkrc.js'));
21
+ if (isLoadableRemarkConfigPath(resolvedRemarkConfigPath)) {
22
+ remarkConfigPath = resolvedRemarkConfigPath;
23
+ }
16
24
  }
17
25
  catch {
18
- remarkConfigPath = cjsRequire.resolve('./remarkrc.js');
26
+ try {
27
+ const resolvedRemarkConfigPath = cjsRequire.resolve('./remarkrc.js');
28
+ if (isLoadableRemarkConfigPath(resolvedRemarkConfigPath)) {
29
+ remarkConfigPath = resolvedRemarkConfigPath;
30
+ }
31
+ }
32
+ catch {
33
+ remarkConfigPath = undefined;
34
+ }
19
35
  }
36
+ const builtRemarkConfigPath = pkgResolve('lib/remarkrc.js');
37
+ if (!remarkConfigPath && fs.existsSync(builtRemarkConfigPath)) {
38
+ remarkConfigPath = builtRemarkConfigPath;
39
+ }
40
+ const globalComponentGlobals = Object.fromEntries(getGlobalComponentNames().map((name) => [name, 'readonly']));
20
41
  async function doom(userConfigOrRoot) {
21
42
  let cspellOptions;
22
43
  let config;
@@ -53,7 +74,7 @@ async function doom(userConfigOrRoot) {
53
74
  languageOptions: {
54
75
  globals: globals.browser,
55
76
  parserOptions: {
56
- remarkConfigPath,
77
+ ...(remarkConfigPath && { remarkConfigPath }),
57
78
  },
58
79
  },
59
80
  rules: cspellEnabled
@@ -67,6 +88,9 @@ async function doom(userConfigOrRoot) {
67
88
  },
68
89
  {
69
90
  files: ['**/*.mdx'],
91
+ languageOptions: {
92
+ globals: globalComponentGlobals,
93
+ },
70
94
  rules: {
71
95
  '@eslint-react/jsx-no-children-prop': 'off',
72
96
  '@eslint-react/rules-of-hooks': 'off',
@@ -0,0 +1,2 @@
1
+ export declare const getGlobalComponentFiles: () => string[];
2
+ export declare const getGlobalComponentNames: () => string[];
@@ -0,0 +1,18 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { baseResolve } from "../../utils/index.js";
4
+ const componentsDir = baseResolve('runtime/components');
5
+ const isGlobalComponentFile = (file) => {
6
+ const basename = path.basename(file, path.extname(file));
7
+ return (!basename.startsWith('_') &&
8
+ !basename.endsWith('.d') &&
9
+ basename !== 'index');
10
+ };
11
+ export const getGlobalComponentFiles = () => fs
12
+ .readdirSync(componentsDir)
13
+ .filter(isGlobalComponentFile)
14
+ .map((file) => path.resolve(componentsDir, file));
15
+ export const getGlobalComponentNames = () => fs
16
+ .readdirSync(componentsDir)
17
+ .filter(isGlobalComponentFile)
18
+ .map((file) => path.basename(file, path.extname(file)));
@@ -3,8 +3,8 @@ import path from 'node:path';
3
3
  import { addTrailingSlash } from '@rspress/shared';
4
4
  import { ACP_BASE } from "../../shared/index.js";
5
5
  import { baseResolve, pkgResolve } from "../../utils/index.js";
6
+ import { getGlobalComponentFiles } from "./components.js";
6
7
  const globalComponentsDir = baseResolve('global');
7
- const componentsDir = baseResolve('runtime/components');
8
8
  export const globalPlugin = ({ version, download, }) => {
9
9
  return {
10
10
  name: 'doom-global',
@@ -13,15 +13,7 @@ export const globalPlugin = ({ version, download, }) => {
13
13
  .readdirSync(globalComponentsDir, 'utf8')
14
14
  .map((component) => path.resolve(globalComponentsDir, component)),
15
15
  markdown: {
16
- globalComponents: fs
17
- .readdirSync(componentsDir)
18
- .filter((file) => {
19
- const basename = path.basename(file, path.extname(file));
20
- return (!basename.startsWith('_') &&
21
- !basename.endsWith('.d') &&
22
- basename !== 'index');
23
- })
24
- .map((file) => path.resolve(componentsDir, file)),
16
+ globalComponents: getGlobalComponentFiles(),
25
17
  },
26
18
  addRuntimeModules(config, isProd) {
27
19
  return {
@@ -13,6 +13,7 @@ export * from './no-empty-table-cell.ts';
13
13
  export * from './no-heading-punctuation.ts';
14
14
  export * from './no-heading-special-characters.ts';
15
15
  export * from './no-heading-sup-sub.ts';
16
+ export * from './no-legacy-os-names.ts';
16
17
  export * from './no-multi-open-api-paths.ts';
17
18
  export * from './no-paragraph-indent.ts';
18
19
  export * from './no-unmatched-anchor.ts';
@@ -12,6 +12,7 @@ export * from "./no-empty-table-cell.js";
12
12
  export * from "./no-heading-punctuation.js";
13
13
  export * from "./no-heading-special-characters.js";
14
14
  export * from "./no-heading-sup-sub.js";
15
+ export * from "./no-legacy-os-names.js";
15
16
  export * from "./no-multi-open-api-paths.js";
16
17
  export * from "./no-paragraph-indent.js";
17
18
  export * from "./no-unmatched-anchor.js";
@@ -0,0 +1,2 @@
1
+ import type { Root } from 'mdast';
2
+ export declare const noLegacyOSNames: import("unified-lint-rule").Plugin<Root, unknown>;
@@ -0,0 +1,51 @@
1
+ import { lintRule } from 'unified-lint-rule';
2
+ import { visitParents } from 'unist-util-visit-parents';
3
+ const LEGACY_OS_NAME_REGEX = /(?<![\p{L}\p{N}])(?:micro\s*os|micros|kube\s*os)(?![\p{L}\p{N}])/giu;
4
+ const isStringValueNode = (node) => !!node &&
5
+ typeof node === 'object' &&
6
+ 'value' in node &&
7
+ typeof node.value === 'string';
8
+ const reportLegacyOSNames = (value, report) => {
9
+ for (const { 0: legacyName } of value.matchAll(LEGACY_OS_NAME_REGEX)) {
10
+ report(legacyName);
11
+ }
12
+ };
13
+ const createMessage = (legacyName) => `Unexpected legacy OS name "${legacyName}", use "Alauda OS" or "alauda os" instead`;
14
+ const getAttributeStringValue = (value) => {
15
+ if (typeof value === 'string') {
16
+ return value;
17
+ }
18
+ return value.value;
19
+ };
20
+ export const noLegacyOSNames = lintRule('doom-lint:no-legacy-os-names', (root, vfile) => {
21
+ visitParents(root, (node, parents) => {
22
+ if (!isStringValueNode(node)) {
23
+ return;
24
+ }
25
+ reportLegacyOSNames(node.value, (legacyName) => {
26
+ vfile.message(createMessage(legacyName), {
27
+ ancestors: [...parents, node],
28
+ place: node.position,
29
+ });
30
+ });
31
+ });
32
+ visitParents(root, ['mdxJsxFlowElement', 'mdxJsxTextElement'], (element, parents) => {
33
+ for (const attribute of element.attributes) {
34
+ if (attribute.type !== 'mdxJsxAttribute' ||
35
+ typeof attribute.value === 'boolean' ||
36
+ attribute.value == null) {
37
+ continue;
38
+ }
39
+ const value = getAttributeStringValue(attribute.value);
40
+ if (value == null) {
41
+ continue;
42
+ }
43
+ reportLegacyOSNames(value, (legacyName) => {
44
+ vfile.message(createMessage(legacyName), {
45
+ ancestors: [...parents, element],
46
+ place: attribute.position,
47
+ });
48
+ });
49
+ }
50
+ });
51
+ });
package/lib/remarkrc.js CHANGED
@@ -10,7 +10,7 @@ import remarkLintLintMatchPunctuation from 'remark-lint-match-punctuation';
10
10
  import remarkLintNoChinesePunctuationInNumber from 'remark-lint-no-chinese-punctuation-in-number';
11
11
  import remarkLintNoDuplicateHeadingsInSection from 'remark-lint-no-duplicate-headings-in-section';
12
12
  import remarkLintNoHiddenTableCell from 'remark-lint-no-hidden-table-cell';
13
- import doomLint, { checkDeadLinks, headingAnchorFormat, noDeepHeading, noDeepList, noMultiOpenAPIPaths, noParagraphIndent, noUnmatchedAnchor, site, tableSize, titleRequired, unitCase, } from "./remark-lint/index.js";
13
+ import doomLint, { checkDeadLinks, headingAnchorFormat, noDeepHeading, noDeepList, noLegacyOSNames, noMultiOpenAPIPaths, noParagraphIndent, noUnmatchedAnchor, site, tableSize, titleRequired, unitCase, } from "./remark-lint/index.js";
14
14
  export default {
15
15
  plugins: [
16
16
  remarkDirective,
@@ -27,6 +27,7 @@ export default {
27
27
  headingAnchorFormat,
28
28
  noDeepHeading,
29
29
  noDeepList,
30
+ noLegacyOSNames,
30
31
  noMultiOpenAPIPaths,
31
32
  noParagraphIndent,
32
33
  noUnmatchedAnchor,
@@ -1,2 +1,3 @@
1
1
  import { type CodeBlockProps } from '@rspress/core/theme-original';
2
+ export declare const getCodeBlockCopyText: (codeBlockElement: Element | null) => string;
2
3
  export declare const CodeBlock: (props: CodeBlockProps) => import("react/jsx-runtime").JSX.Element;
@@ -1,4 +1,66 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { CodeBlock as OriginalCodeBlock, } from '@rspress/core/theme-original';
2
+ import { CodeBlock as OriginalCodeBlock, copyToClipboard, } from '@rspress/core/theme-original';
3
+ import { useCallback } from 'react';
3
4
  import { AutoExpandable } from './AutoExpandable.js';
4
- export const CodeBlock = (props) => (_jsx(AutoExpandable, { children: _jsx(OriginalCodeBlock, { ...props }) }));
5
+ const COPIED_CLASS = 'rp-code-copy-button--copied';
6
+ const COPY_IGNORE_SELECTOR = '.rp-copy-ignore, .linenumber';
7
+ const timeoutIdMap = new WeakMap();
8
+ const getCopySourceElement = (codeBlockElement) => codeBlockElement?.querySelector('pre code') ??
9
+ codeBlockElement?.querySelector('code') ??
10
+ codeBlockElement?.querySelector('pre') ??
11
+ null;
12
+ const removeIgnoredElements = (element) => {
13
+ for (const ignoredElement of element.querySelectorAll(COPY_IGNORE_SELECTOR)) {
14
+ ignoredElement.remove();
15
+ }
16
+ };
17
+ export const getCodeBlockCopyText = (codeBlockElement) => {
18
+ const sourceElement = getCopySourceElement(codeBlockElement);
19
+ if (!sourceElement) {
20
+ return '';
21
+ }
22
+ const clonedSourceElement = sourceElement.cloneNode(true);
23
+ if (!(clonedSourceElement instanceof Element)) {
24
+ return sourceElement.textContent;
25
+ }
26
+ removeIgnoredElements(clonedSourceElement);
27
+ const lines = clonedSourceElement.querySelectorAll('.line');
28
+ if (lines.length) {
29
+ return Array.from(lines, (line) => line.textContent).join('\n');
30
+ }
31
+ return clonedSourceElement.textContent;
32
+ };
33
+ const markCopied = (copyButtonElement) => {
34
+ copyButtonElement.classList.add(COPIED_CLASS);
35
+ const timeoutId = timeoutIdMap.get(copyButtonElement);
36
+ if (timeoutId) {
37
+ clearTimeout(timeoutId);
38
+ }
39
+ timeoutIdMap.set(copyButtonElement, setTimeout(() => {
40
+ copyButtonElement.classList.remove(COPIED_CLASS);
41
+ copyButtonElement.blur();
42
+ timeoutIdMap.delete(copyButtonElement);
43
+ }, 2000));
44
+ };
45
+ const copyCodeBlock = async (codeBlockElement, copyButtonElement) => {
46
+ const text = getCodeBlockCopyText(codeBlockElement);
47
+ const isCopied = await copyToClipboard(text);
48
+ if (isCopied) {
49
+ markCopied(copyButtonElement);
50
+ }
51
+ };
52
+ export const CodeBlock = (props) => {
53
+ const onClickCapture = useCallback((event) => {
54
+ if (!(event.target instanceof Element)) {
55
+ return;
56
+ }
57
+ const copyButtonElement = event.target.closest('.rp-code-copy-button');
58
+ if (!copyButtonElement) {
59
+ return;
60
+ }
61
+ event.preventDefault();
62
+ event.stopPropagation();
63
+ void copyCodeBlock(copyButtonElement.closest('.rp-codeblock'), copyButtonElement);
64
+ }, []);
65
+ return (_jsx(AutoExpandable, { children: _jsx("div", { onClickCapture: onClickCapture, children: _jsx(OriginalCodeBlock, { ...props }) }) }));
66
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "2.5.2",
3
+ "version": "2.5.4",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {
@@ -44,7 +44,7 @@
44
44
  ],
45
45
  "dependencies": {
46
46
  "@alauda/doc-stream-sdk": "0.1.0",
47
- "@alauda/doom-export": "^0.4.1",
47
+ "@alauda/doom-export": "^0.4.2",
48
48
  "@cspell/eslint-plugin": "^10.0.0",
49
49
  "@eslint-react/eslint-plugin": "^5.8.3",
50
50
  "@eslint/js": "^10.0.1",