@alauda/doom 0.2.6 → 0.3.0

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,15 +1,17 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
1
3
  import { nodeTypes } from '@mdx-js/mdx';
2
4
  import { addLeadingSlash, addTrailingSlash, removeLeadingSlash, } from '@rspress/core';
3
5
  import { pluginPreview } from '@rspress/plugin-preview';
6
+ import { createTransformerDiff, createTransformerErrorLevel, createTransformerFocus, createTransformerHighlight, pluginShiki, } from '@rspress/plugin-shiki';
4
7
  import { logger } from '@rspress/shared/logger';
5
8
  import getPort from 'get-port';
6
- import fs from 'node:fs';
7
- import path from 'node:path';
8
9
  import rehypeRaw from 'rehype-raw';
9
10
  import { parse } from 'yaml';
10
11
  import { apiPlugin, autoSidebarPlugin, globalPlugin } from '../plugins/index.js';
11
12
  import { pkgResolve } from '../utils/helpers.js';
12
13
  import { CWD, DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS, I18N_FILE, YAML_EXTENSIONS, } from './constants.js';
14
+ import { createTransformerCallouts } from './shiki/index.js';
13
15
  const DEFAULT_LOGO = '/logo.svg';
14
16
  const getCommonConfig = async (config) => {
15
17
  return {
@@ -26,6 +28,7 @@ const getCommonConfig = async (config) => {
26
28
  },
27
29
  markdown: {
28
30
  checkDeadLinks: true,
31
+ defaultWrapCode: true,
29
32
  highlightLanguages: [['jsonc', 'json']],
30
33
  mdxRs: false,
31
34
  rehypePlugins: [[rehypeRaw, { passThrough: nodeTypes }]],
@@ -61,6 +64,17 @@ const getCommonConfig = async (config) => {
61
64
  },
62
65
  defaultRenderMode: 'pure',
63
66
  }),
67
+ pluginShiki({
68
+ transformers: [
69
+ // builtin transformers
70
+ createTransformerDiff(),
71
+ createTransformerErrorLevel(),
72
+ createTransformerFocus(),
73
+ createTransformerHighlight(),
74
+ // custom transformers
75
+ createTransformerCallouts(),
76
+ ],
77
+ }),
64
78
  autoSidebarPlugin(),
65
79
  globalPlugin(),
66
80
  apiPlugin(),
@@ -0,0 +1 @@
1
+ export * from './transformers/index.js';
@@ -0,0 +1 @@
1
+ export * from './transformers/index.js';
@@ -0,0 +1,7 @@
1
+ import { PluginShikiOptions } from '@rspress/plugin-shiki';
2
+ import { IRangeTransformerOptions } from '../types.js';
3
+ export interface ITransformerCalloutsOptions extends IRangeTransformerOptions {
4
+ classActivePre?: string;
5
+ classActiveLine?: string;
6
+ }
7
+ export declare const createTransformerCallouts: (options?: ITransformerCalloutsOptions) => Exclude<PluginShikiOptions["transformers"], undefined>[0];
@@ -0,0 +1,16 @@
1
+ import { addClass, checkClass, createRangeTransformer } from '../utils.js';
2
+ export const createTransformerCallouts = (options = {}) => {
3
+ const { classActiveLine = 'callout', classActivePre = 'has-callouts' } = options;
4
+ return {
5
+ name: 'shiki-transformer:callouts',
6
+ preTransformer: createRangeTransformer({
7
+ callout: classActiveLine,
8
+ }, options),
9
+ postTransformer({ code }) {
10
+ if (!checkClass(code, classActiveLine)) {
11
+ return code;
12
+ }
13
+ return addClass(code, classActivePre, 'pre');
14
+ },
15
+ };
16
+ };
@@ -0,0 +1 @@
1
+ export * from './callouts.js';
@@ -0,0 +1 @@
1
+ export * from './callouts.js';
@@ -0,0 +1,14 @@
1
+ import type { HtmlRendererOptions } from 'shiki';
2
+ export type TLineOptions = NonNullable<HtmlRendererOptions['lineOptions']>;
3
+ export interface ITransformerResult {
4
+ code: string;
5
+ lineOptions: TLineOptions;
6
+ }
7
+ export interface ITransformerOptions {
8
+ code: string;
9
+ lang: string;
10
+ }
11
+ export type TPreTransformer = (options: ITransformerOptions) => ITransformerResult;
12
+ export interface IRangeTransformerOptions {
13
+ tagRegExp?: RegExp;
14
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { IRangeTransformerOptions, TPreTransformer } from './types.js';
2
+ export declare function createRangeTransformer(classMap: Record<string, string | string[]>, options?: IRangeTransformerOptions): TPreTransformer;
3
+ export declare const checkClass: (code: string, className: string | string[]) => boolean;
4
+ export declare function addClass(code: string, classes: string | string[], tag?: string): string;
@@ -0,0 +1,47 @@
1
+ export function createRangeTransformer(classMap, options = {}) {
2
+ return ({ code }) => {
3
+ // https://regex101.com/r/mUxvfx/1
4
+ const tagRE = options.tagRegExp ??
5
+ /(?:#|\/\/|\/\*{1,2}) *\[!code ([\w+-]+)(?::(\d+))?] *(?:\*{1,2}\/)?/;
6
+ const lineOptions = [];
7
+ const tags = Object.keys(classMap);
8
+ const codeFormat = code
9
+ .split('\n')
10
+ .map((lineOfCode, lineNumber) => {
11
+ const [match, tag, range] = lineOfCode.match(tagRE) ?? [];
12
+ if (!match) {
13
+ return lineOfCode;
14
+ }
15
+ if (!tags.includes(tag)) {
16
+ return lineOfCode;
17
+ }
18
+ for (const [rangeOffset] of Array.from({
19
+ length: Number(range ?? 1),
20
+ }).entries()) {
21
+ lineOptions.push({
22
+ line: lineNumber + rangeOffset + 1,
23
+ classes: typeof classMap[tag] === 'string'
24
+ ? [classMap[tag]]
25
+ : classMap[tag],
26
+ });
27
+ }
28
+ return lineOfCode.replace(tagRE, '');
29
+ })
30
+ .join('\n');
31
+ return {
32
+ code: codeFormat,
33
+ lineOptions,
34
+ };
35
+ };
36
+ }
37
+ export const checkClass = (code, className) => {
38
+ const classes = Array.isArray(className) ? className.join(' ') : className;
39
+ return code.search(classes) !== -1;
40
+ };
41
+ export function addClass(code, classes, tag) {
42
+ const classRE = new RegExp(`<${tag ?? 'w+'}[^>]*class="([\\w+-:;\\/* ]*)"`);
43
+ classes = Array.isArray(classes) ? classes : [classes];
44
+ return code.replace(classRE, (match, previousClasses) => {
45
+ return match.replace(previousClasses, `${previousClasses} ${classes.join(' ')}`.trim());
46
+ });
47
+ }
@@ -14,10 +14,12 @@ const sidebarSorter = (a, b) => {
14
14
  */
15
15
  const splitSideMeta = (sideMeta, extensions) => {
16
16
  const result = sideMeta.reduce((acc, curr) => {
17
+ let fileParts;
17
18
  if ('_fileKey' in curr &&
18
19
  !('items' in curr) &&
19
20
  !acc.index &&
20
- extensions.some((ext) => curr._fileKey?.endsWith(`/index${ext}`))) {
21
+ (fileParts = curr._fileKey?.split('/')) &&
22
+ extensions.some((ext) => fileParts.at(-1) === `index${ext}`)) {
21
23
  acc.index = curr;
22
24
  }
23
25
  else {
@@ -185,7 +187,6 @@ export async function walk(workDir, routePrefix = '/', docsDir, extensions) {
185
187
  });
186
188
  const { index, others } = await scanSideMeta(workDir, workDir, docsDir, routePrefix, extensions, ['assets', 'public', 'shared']);
187
189
  const sidebars = index ? [index, ...others] : others;
188
- sidebars.sort(sidebarSorter);
189
190
  // Every sub dir will represent a group of sidebar
190
191
  const sidebarConfig = {
191
192
  [routePrefix]: sidebars,
@@ -0,0 +1,6 @@
1
+ import { ReactNode } from 'react';
2
+ export interface CalloutsProps {
3
+ children: ReactNode;
4
+ }
5
+ export declare const Callouts: ({ children }: CalloutsProps) => import("react/jsx-runtime").JSX.Element;
6
+ export default Callouts;
@@ -0,0 +1,5 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ export const Callouts = ({ children }) => {
3
+ return _jsx("p", { className: "my-4 leading-7 doom-callouts", children: children });
4
+ };
5
+ export default Callouts;
@@ -1,3 +1,4 @@
1
+ export * from './Callouts.js';
1
2
  export * from './K8sCrd.js';
2
3
  export * from './Overview.js';
3
4
  export * from './QuickNav.js';
@@ -1,3 +1,4 @@
1
+ export * from './Callouts.js';
1
2
  export * from './K8sCrd.js';
2
3
  export * from './Overview.js';
3
4
  export * from './QuickNav.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.2.6",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {
@@ -38,12 +38,13 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@openapi-contrib/openapi-schema-to-json-schema": "^5.1.0",
41
- "@rspress/core": "^1.37.4",
42
- "@rspress/plugin-preview": "^1.37.4",
41
+ "@rspress/core": "^1.38.0",
42
+ "@rspress/plugin-preview": "^1.38.0",
43
+ "@rspress/plugin-shiki": "^1.38.0",
43
44
  "chokidar": "^4.0.1",
44
45
  "clsx": "^2.1.1",
45
46
  "commander": "^12.1.0",
46
- "es-toolkit": "^1.29.0",
47
+ "es-toolkit": "^1.30.1",
47
48
  "get-port": "^7.1.0",
48
49
  "react-markdown": "^8.0.7",
49
50
  "rehype-raw": "^6.1.1",
@@ -54,15 +55,15 @@
54
55
  "yoctocolors": "^2.1.1"
55
56
  },
56
57
  "devDependencies": {
57
- "@eslint/js": "^9.16.0",
58
+ "@eslint/js": "^9.17.0",
58
59
  "@swc-node/register": "^1.10.9",
59
60
  "@swc/core": "1.10.1",
60
61
  "@types/eslint__js": "^8.42.3",
61
62
  "@types/fs-extra": "^11.0.4",
62
63
  "@types/node": "~18.17.19",
63
64
  "@types/swagger2openapi": "^7.0.4",
64
- "eslint": "^9.16.0",
65
- "lint-staged": "^15.2.10",
65
+ "eslint": "^9.17.0",
66
+ "lint-staged": "^15.2.11",
66
67
  "npm-run-all2": "^7.0.1",
67
68
  "openapi-types": "^12.1.3",
68
69
  "prettier": "^3.4.2",
@@ -37,3 +37,49 @@ td {
37
37
  }
38
38
  }
39
39
  }
40
+
41
+ @mixin callout {
42
+ counter-increment: callouts;
43
+ content: counter(callouts);
44
+ border-radius: 50%;
45
+ background-color: #06c;
46
+ color: #fff;
47
+ width: 20px;
48
+ height: 20px;
49
+ display: inline-flex;
50
+ justify-content: center;
51
+ align-items: center;
52
+ }
53
+
54
+ .has-callouts .rspress-code-content {
55
+ counter-reset: callouts;
56
+
57
+ .line.callout {
58
+ &:after {
59
+ @include callout;
60
+ }
61
+ }
62
+ }
63
+
64
+ .doom-callouts {
65
+ > ol {
66
+ counter-reset: callouts;
67
+ list-style-type: none;
68
+
69
+ > li {
70
+ margin-left: -1.25rem;
71
+
72
+ &:before {
73
+ @include callout;
74
+ margin-right: 0.75rem;
75
+ }
76
+
77
+ > {
78
+ ul,
79
+ ol {
80
+ margin-left: 1.25rem;
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }