@alauda/doom 0.3.0 → 0.3.1

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.
@@ -3,15 +3,14 @@ import path from 'node:path';
3
3
  import { nodeTypes } from '@mdx-js/mdx';
4
4
  import { addLeadingSlash, addTrailingSlash, removeLeadingSlash, } from '@rspress/core';
5
5
  import { pluginPreview } from '@rspress/plugin-preview';
6
- import { createTransformerDiff, createTransformerErrorLevel, createTransformerFocus, createTransformerHighlight, pluginShiki, } from '@rspress/plugin-shiki';
7
6
  import { logger } from '@rspress/shared/logger';
7
+ import { transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationWordHighlight, transformerRemoveNotationEscape, } from '@shikijs/transformers';
8
8
  import getPort from 'get-port';
9
9
  import rehypeRaw from 'rehype-raw';
10
10
  import { parse } from 'yaml';
11
- import { apiPlugin, autoSidebarPlugin, globalPlugin } from '../plugins/index.js';
11
+ import { apiPlugin, autoSidebarPlugin, createTransformerCallouts, globalPlugin, shikiPlugin, } from '../plugins/index.js';
12
12
  import { pkgResolve } from '../utils/helpers.js';
13
13
  import { CWD, DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS, I18N_FILE, YAML_EXTENSIONS, } from './constants.js';
14
- import { createTransformerCallouts } from './shiki/index.js';
15
14
  const DEFAULT_LOGO = '/logo.svg';
16
15
  const getCommonConfig = async (config) => {
17
16
  return {
@@ -28,7 +27,6 @@ const getCommonConfig = async (config) => {
28
27
  },
29
28
  markdown: {
30
29
  checkDeadLinks: true,
31
- defaultWrapCode: true,
32
30
  highlightLanguages: [['jsonc', 'json']],
33
31
  mdxRs: false,
34
32
  rehypePlugins: [[rehypeRaw, { passThrough: nodeTypes }]],
@@ -64,20 +62,25 @@ const getCommonConfig = async (config) => {
64
62
  },
65
63
  defaultRenderMode: 'pure',
66
64
  }),
67
- pluginShiki({
65
+ apiPlugin(),
66
+ autoSidebarPlugin(),
67
+ globalPlugin(),
68
+ shikiPlugin({
69
+ langs: ['dockerfile', 'go', 'jsonc'],
68
70
  transformers: [
69
71
  // builtin transformers
70
- createTransformerDiff(),
71
- createTransformerErrorLevel(),
72
- createTransformerFocus(),
73
- createTransformerHighlight(),
72
+ transformerMetaHighlight(),
73
+ transformerMetaWordHighlight(),
74
+ transformerNotationDiff(),
75
+ transformerNotationErrorLevel(),
76
+ transformerNotationFocus(),
77
+ transformerNotationHighlight(),
78
+ transformerNotationWordHighlight(),
79
+ transformerRemoveNotationEscape(),
74
80
  // custom transformers
75
81
  createTransformerCallouts(),
76
82
  ],
77
83
  }),
78
- autoSidebarPlugin(),
79
- globalPlugin(),
80
- apiPlugin(),
81
84
  ],
82
85
  builderConfig: {
83
86
  server: {
@@ -187,6 +187,11 @@ export async function walk(workDir, routePrefix = '/', docsDir, extensions) {
187
187
  });
188
188
  const { index, others } = await scanSideMeta(workDir, workDir, docsDir, routePrefix, extensions, ['assets', 'public', 'shared']);
189
189
  const sidebars = index ? [index, ...others] : others;
190
+ for (const sidebarItem of sidebars) {
191
+ if ('items' in sidebarItem && sidebarItem.items.length) {
192
+ sidebarItem.collapsed = false;
193
+ }
194
+ }
190
195
  // Every sub dir will represent a group of sidebar
191
196
  const sidebarConfig = {
192
197
  [routePrefix]: sidebars,
@@ -1,3 +1,4 @@
1
+ export * from './api/index.js';
1
2
  export * from './auto-sidebar/index.js';
2
3
  export * from './global/index.js';
3
- export * from './api/index.js';
4
+ export * from './shiki/index.js';
@@ -1,3 +1,4 @@
1
+ export * from './api/index.js';
1
2
  export * from './auto-sidebar/index.js';
2
3
  export * from './global/index.js';
3
- export * from './api/index.js';
4
+ export * from './shiki/index.js';
@@ -0,0 +1,5 @@
1
+ import { type BuiltinLanguage, type BuiltinTheme, type Highlighter, type BundledHighlighterOptions as ShikiHighlighterOptions, type ShikiTransformer } from 'shiki';
2
+ export interface HighlighterOptions extends ShikiHighlighterOptions<BuiltinLanguage, BuiltinTheme> {
3
+ transformers?: ShikiTransformer[];
4
+ }
5
+ export declare function getHighlighter(options: HighlighterOptions): Promise<Highlighter>;
@@ -0,0 +1,15 @@
1
+ import { createHighlighter as createShikiHighlighter, } from 'shiki';
2
+ export async function getHighlighter(options) {
3
+ const highlighter = await createShikiHighlighter(options);
4
+ const baseTransformers = options.transformers ?? [];
5
+ return {
6
+ ...highlighter,
7
+ codeToHtml: (code, htmlOptions) => {
8
+ const transformers = htmlOptions.transformers ?? [];
9
+ return highlighter.codeToHtml(code, {
10
+ ...htmlOptions,
11
+ transformers: [...baseTransformers, ...transformers],
12
+ });
13
+ },
14
+ };
15
+ }
@@ -0,0 +1,22 @@
1
+ import type { RspressPlugin } from '@rspress/shared';
2
+ import { type BuiltinLanguage, type BuiltinTheme, type ShikiTransformer, type SpecialLanguage } from 'shiki';
3
+ export interface PluginShikiOptions {
4
+ /**
5
+ * Code highlighting theme, @see https://shiki.style/themes
6
+ */
7
+ theme?: BuiltinTheme | 'css-variables';
8
+ /**
9
+ * Code highlighting language, @see https://shiki.style/languages
10
+ */
11
+ langs?: Array<BuiltinLanguage | SpecialLanguage>;
12
+ /**
13
+ * Custom shiki transformer, @see https://shiki.style/guide/transformers
14
+ */
15
+ transformers?: ShikiTransformer[];
16
+ }
17
+ export declare const SHIKI_DEFAULT_HIGHLIGHT_LANGUAGES: BuiltinLanguage[];
18
+ /**
19
+ * The plugin is used to add the last updated time to the page.
20
+ */
21
+ export declare function shikiPlugin({ theme, langs, transformers, }?: PluginShikiOptions): RspressPlugin;
22
+ export * from './transformers/index.js';
@@ -0,0 +1,61 @@
1
+ import { dirname, join } from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { createCssVariablesTheme, } from 'shiki';
4
+ import { rehypePluginShiki } from './rehypePlugin.js';
5
+ import { SHIKI_TRANSFORMER_LINE_NUMBER, createTransformerLineNumber, } from './transformers/line-number.js';
6
+ import { getHighlighter } from './highlighter.js';
7
+ const __filename = fileURLToPath(import.meta.url);
8
+ const __dirname = dirname(__filename);
9
+ export const SHIKI_DEFAULT_HIGHLIGHT_LANGUAGES = [
10
+ 'js',
11
+ 'ts',
12
+ 'jsx',
13
+ 'tsx',
14
+ 'json',
15
+ 'css',
16
+ 'scss',
17
+ 'less',
18
+ 'xml',
19
+ 'diff',
20
+ 'yaml',
21
+ 'md',
22
+ 'mdx',
23
+ 'bash',
24
+ ];
25
+ const cssVariablesTheme = createCssVariablesTheme({
26
+ name: 'css-variables',
27
+ variablePrefix: '--shiki-',
28
+ variableDefaults: {},
29
+ fontStyle: true,
30
+ });
31
+ /**
32
+ * The plugin is used to add the last updated time to the page.
33
+ */
34
+ export function shikiPlugin({ theme = 'css-variables', langs = [], transformers = [], } = {}) {
35
+ return {
36
+ name: 'doom-shiki',
37
+ async config(config) {
38
+ config.markdown = config.markdown || {};
39
+ // Shiki will be integrated by rehype plugin, so we should use the javascript version markdown compiler.
40
+ config.markdown.mdxRs = false;
41
+ config.markdown.codeHighlighter = 'shiki';
42
+ config.markdown.rehypePlugins = config.markdown.rehypePlugins || [];
43
+ if (config.markdown.showLineNumbers &&
44
+ !transformers.some((transformerItem) => transformerItem.name === SHIKI_TRANSFORMER_LINE_NUMBER)) {
45
+ transformers.push(createTransformerLineNumber());
46
+ }
47
+ const highlighter = await getHighlighter({
48
+ themes: [cssVariablesTheme],
49
+ langs: [...SHIKI_DEFAULT_HIGHLIGHT_LANGUAGES, ...langs],
50
+ transformers,
51
+ });
52
+ config.markdown.rehypePlugins.push([
53
+ rehypePluginShiki,
54
+ { highlighter, theme },
55
+ ]);
56
+ return config;
57
+ },
58
+ globalStyles: join(__dirname, './shiki.css'),
59
+ };
60
+ }
61
+ export * from './transformers/index.js';
@@ -0,0 +1,9 @@
1
+ import type { Plugin } from 'unified';
2
+ import type { Root } from 'hast';
3
+ import type { BuiltinTheme, Highlighter } from 'shiki';
4
+ interface Options {
5
+ highlighter: Highlighter;
6
+ theme: BuiltinTheme;
7
+ }
8
+ export declare const rehypePluginShiki: Plugin<[Options], Root>;
9
+ export {};
@@ -0,0 +1,47 @@
1
+ import { visit } from 'unist-util-visit';
2
+ import { fromHtml } from 'hast-util-from-html';
3
+ // TODO: migrate to official @shikijs/rehype plugin after upgrading unified/remark/rehype packages
4
+ export const rehypePluginShiki = ({ highlighter, theme }) => (tree) => {
5
+ visit(tree, 'element', (node, index, parent) => {
6
+ // <pre><code>...</code></pre>
7
+ if (node.tagName === 'pre' &&
8
+ node.children[0]?.type === 'element' &&
9
+ node.children[0].tagName === 'code') {
10
+ const codeNode = node.children[0];
11
+ const codeContent = codeNode.children[0].value;
12
+ const codeClassName = codeNode.properties?.className?.toString() || '';
13
+ const codeMeta = codeNode.properties?.meta?.toString() || '';
14
+ // for example: language-js {1,2,3-5}
15
+ const lang = codeClassName.split(' ')[0].split('-')[1];
16
+ if (!lang) {
17
+ return;
18
+ }
19
+ const highlightedCode = highlighter.codeToHtml(codeContent, {
20
+ lang,
21
+ theme,
22
+ meta: {
23
+ __raw: codeMeta,
24
+ },
25
+ });
26
+ const fragmentAst = fromHtml(highlightedCode, { fragment: true });
27
+ const preElement = fragmentAst.children[0];
28
+ const codeElement = preElement.children[0];
29
+ codeElement.properties.className = `language-${lang}`;
30
+ codeElement.properties.meta = codeMeta;
31
+ const codeLines = codeElement.children;
32
+ // Strip the final empty span
33
+ const lastLine = codeLines[codeLines.length - 1];
34
+ if (lastLine.children.length === 0) {
35
+ codeLines.pop();
36
+ }
37
+ parent?.children.splice(index, 1, {
38
+ type: 'element',
39
+ tagName: 'pre',
40
+ properties: {
41
+ className: 'code',
42
+ },
43
+ children: fragmentAst.children,
44
+ });
45
+ }
46
+ });
47
+ };
@@ -0,0 +1,6 @@
1
+ import type { ShikiTransformer } from 'shiki';
2
+ export interface ITransformerCalloutsOptions {
3
+ classActivePre?: string;
4
+ classActiveLine?: string;
5
+ }
6
+ export declare const createTransformerCallouts: (options?: ITransformerCalloutsOptions) => ShikiTransformer;
@@ -0,0 +1,10 @@
1
+ import { transformerNotationMap } from '@shikijs/transformers';
2
+ export const createTransformerCallouts = (options = {}) => {
3
+ const { classActiveLine = 'callout', classActivePre = 'has-callouts' } = options;
4
+ return transformerNotationMap({
5
+ classMap: {
6
+ callout: classActiveLine,
7
+ },
8
+ classActivePre,
9
+ }, 'shiki-transformer:callouts');
10
+ };
@@ -0,0 +1,2 @@
1
+ export * from './callouts.js';
2
+ export * from './line-number.js';
@@ -0,0 +1,2 @@
1
+ export * from './callouts.js';
2
+ export * from './line-number.js';
@@ -0,0 +1,7 @@
1
+ import type { ShikiTransformer } from 'shiki';
2
+ export interface ITransformerLineNumberOptions {
3
+ classActivePre?: string;
4
+ classActiveLine?: string;
5
+ }
6
+ export declare const SHIKI_TRANSFORMER_LINE_NUMBER = "shiki-transformer:line-number";
7
+ export declare function createTransformerLineNumber(options?: ITransformerLineNumberOptions): ShikiTransformer;
@@ -0,0 +1,13 @@
1
+ export const SHIKI_TRANSFORMER_LINE_NUMBER = 'shiki-transformer:line-number';
2
+ export function createTransformerLineNumber(options = {}) {
3
+ const { classActiveLine = 'line-number', classActivePre = 'has-line-number', } = options;
4
+ return {
5
+ name: SHIKI_TRANSFORMER_LINE_NUMBER,
6
+ pre(pre) {
7
+ return this.addClassToHast(pre, classActivePre);
8
+ },
9
+ line(node) {
10
+ this.addClassToHast(node, classActiveLine);
11
+ },
12
+ };
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {
@@ -40,7 +40,7 @@
40
40
  "@openapi-contrib/openapi-schema-to-json-schema": "^5.1.0",
41
41
  "@rspress/core": "^1.38.0",
42
42
  "@rspress/plugin-preview": "^1.38.0",
43
- "@rspress/plugin-shiki": "^1.38.0",
43
+ "@shikijs/transformers": "^1.24.2",
44
44
  "chokidar": "^4.0.1",
45
45
  "clsx": "^2.1.1",
46
46
  "commander": "^12.1.0",
@@ -49,6 +49,7 @@
49
49
  "react-markdown": "^8.0.7",
50
50
  "rehype-raw": "^6.1.1",
51
51
  "remark-gfm": "^3.0.1",
52
+ "shiki": "^1.24.2",
52
53
  "swagger2openapi": "^7.0.8",
53
54
  "tinyglobby": "^0.2.10",
54
55
  "yaml": "^2.6.1",