@docubook/core 1.7.2 → 1.8.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.
Files changed (56) hide show
  1. package/README.md +25 -1
  2. package/dist/chunk-HZJLRYAI.js +45 -0
  3. package/dist/chunk-HZJLRYAI.js.map +1 -0
  4. package/dist/index.d.ts +118 -10
  5. package/dist/index.js +514 -6
  6. package/dist/index.js.map +1 -1
  7. package/dist/utils.d.ts +12 -10
  8. package/dist/utils.js +16 -38
  9. package/dist/utils.js.map +1 -1
  10. package/package.json +5 -3
  11. package/dist/__tests__/compile.test.d.ts +0 -2
  12. package/dist/__tests__/compile.test.d.ts.map +0 -1
  13. package/dist/__tests__/compile.test.js +0 -122
  14. package/dist/__tests__/compile.test.js.map +0 -1
  15. package/dist/__tests__/content.test.d.ts +0 -2
  16. package/dist/__tests__/content.test.d.ts.map +0 -1
  17. package/dist/__tests__/content.test.js +0 -109
  18. package/dist/__tests__/content.test.js.map +0 -1
  19. package/dist/__tests__/extract.test.d.ts +0 -2
  20. package/dist/__tests__/extract.test.d.ts.map +0 -1
  21. package/dist/__tests__/extract.test.js +0 -95
  22. package/dist/__tests__/extract.test.js.map +0 -1
  23. package/dist/__tests__/plugins/handleCodeExpandable.test.d.ts +0 -2
  24. package/dist/__tests__/plugins/handleCodeExpandable.test.d.ts.map +0 -1
  25. package/dist/__tests__/plugins/handleCodeExpandable.test.js +0 -191
  26. package/dist/__tests__/plugins/handleCodeExpandable.test.js.map +0 -1
  27. package/dist/__tests__/plugins/handleCodeTitles.test.d.ts +0 -2
  28. package/dist/__tests__/plugins/handleCodeTitles.test.d.ts.map +0 -1
  29. package/dist/__tests__/plugins/handleCodeTitles.test.js +0 -99
  30. package/dist/__tests__/plugins/handleCodeTitles.test.js.map +0 -1
  31. package/dist/compile.d.ts +0 -28
  32. package/dist/compile.d.ts.map +0 -1
  33. package/dist/compile.js +0 -88
  34. package/dist/compile.js.map +0 -1
  35. package/dist/content.d.ts +0 -52
  36. package/dist/content.d.ts.map +0 -1
  37. package/dist/content.js +0 -102
  38. package/dist/content.js.map +0 -1
  39. package/dist/extract.d.ts +0 -13
  40. package/dist/extract.d.ts.map +0 -1
  41. package/dist/extract.js +0 -96
  42. package/dist/extract.js.map +0 -1
  43. package/dist/index.d.ts.map +0 -1
  44. package/dist/plugins/handleCodeExpandable.d.ts +0 -4
  45. package/dist/plugins/handleCodeExpandable.d.ts.map +0 -1
  46. package/dist/plugins/handleCodeExpandable.js +0 -174
  47. package/dist/plugins/handleCodeExpandable.js.map +0 -1
  48. package/dist/plugins/handleCodeTitles.d.ts +0 -3
  49. package/dist/plugins/handleCodeTitles.d.ts.map +0 -1
  50. package/dist/plugins/handleCodeTitles.js +0 -38
  51. package/dist/plugins/handleCodeTitles.js.map +0 -1
  52. package/dist/types.d.ts +0 -12
  53. package/dist/types.d.ts.map +0 -1
  54. package/dist/types.js +0 -2
  55. package/dist/types.js.map +0 -1
  56. package/dist/utils.d.ts.map +0 -1
package/README.md CHANGED
@@ -89,6 +89,7 @@ const tocs = await docsService.getTocsForSlug("getting-started/introduction");
89
89
  | `handleCodeTitles` | Move code title metadata to `<pre>` attributes (advanced) | transformer function |
90
90
  | `handleCodeExpandableRemark` | Remark plugin that detects `Expandable` meta on code blocks and injects expandable data attributes | transformer function |
91
91
  | `handleCodeExpandable` | Rehype plugin that propagates expandable metadata from `<code>` to `<pre>` elements | transformer function |
92
+ | `rehypeMermaid` | Rehype plugin that transforms ` ```mermaid ` fenced code blocks into `<Mermaid chart="...">` elements | transformer function |
92
93
  | `serialize` | Re-exported from `next-mdx-remote/serialize` for non-RSC MDX compilation workflows | `MDXRemoteSerializeResult` |
93
94
  | `MDXRemote` | Re-exported from `next-mdx-remote` for client-side MDX hydration | React component |
94
95
  | `cn` | Merge class names using `clsx` + `tailwind-merge` | `string` |
@@ -254,7 +255,30 @@ const mdxSource = await serialize(rawMdx, { parseFrontmatter: true });
254
255
 
255
256
  Use this for Pages Router or non-RSC environments where `compileMDX` (used internally by `parseMdx`) is not available. `serialize` compiles MDX on the server and `MDXRemote` hydrates it on the client.
256
257
 
257
- #### 12. Expandable code blocks (code plugins)
258
+ #### 12. Mermaid diagram support (`rehypeMermaid`)
259
+
260
+ ```ts
261
+ import { rehypeMermaid } from "@docubook/core";
262
+ ```
263
+
264
+ This plugin transforms ` ```mermaid ` fenced code blocks into `<Mermaid chart="...">` elements during MDX compilation. It runs after `preProcess` and before other code transforms, so mermaid blocks are converted before prism or expandable-code plugins process them.
265
+
266
+ The plugin is included in `createDefaultRehypePlugins()` by default — no manual registration needed.
267
+
268
+ In your MDX files:
269
+
270
+ ````md
271
+ ```mermaid
272
+ graph TD
273
+ A[Start] --> B{Decision}
274
+ B -->|Yes| C[Process]
275
+ B -->|No| D[End]
276
+ ```
277
+ ````
278
+
279
+ This requires the `<Mermaid>` component to be registered in your MDX component map (provided by `@docubook/mdx-content`).
280
+
281
+ #### 13. Expandable code blocks (code plugins)
258
282
 
259
283
  ```ts
260
284
  import {
@@ -0,0 +1,45 @@
1
+ // src/utils.ts
2
+ import { clsx } from "clsx";
3
+ import { twMerge } from "tailwind-merge";
4
+ function cn(...inputs) {
5
+ return twMerge(clsx(inputs));
6
+ }
7
+ function parseDate(dateStr) {
8
+ if (/^\d{4}-/.test(dateStr)) return new Date(dateStr);
9
+ const [day, month, year] = dateStr.split("-").map(Number);
10
+ return new Date(year, month - 1, day);
11
+ }
12
+ function stringToDate(date) {
13
+ return date instanceof Date ? date : parseDate(date);
14
+ }
15
+ function formatDate(dateStrOrDate) {
16
+ const date = stringToDate(dateStrOrDate);
17
+ return date.toLocaleDateString("en-US", {
18
+ weekday: "long",
19
+ year: "numeric",
20
+ month: "long",
21
+ day: "numeric"
22
+ });
23
+ }
24
+ function formatDate2(dateStrOrDate) {
25
+ const date = stringToDate(dateStrOrDate);
26
+ return date.toLocaleDateString("en-US", {
27
+ month: "short",
28
+ day: "numeric",
29
+ year: "numeric"
30
+ });
31
+ }
32
+ function toIsoDateOnly(dateStrOrDate) {
33
+ const date = stringToDate(dateStrOrDate);
34
+ return date.toISOString().slice(0, 10);
35
+ }
36
+
37
+ export {
38
+ cn,
39
+ parseDate,
40
+ stringToDate,
41
+ formatDate,
42
+ formatDate2,
43
+ toIsoDateOnly
44
+ };
45
+ //# sourceMappingURL=chunk-HZJLRYAI.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { type ClassValue, clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\nimport type { Node } from \"unist\";\n\nexport interface ElementNode extends Node {\n type: string;\n tagName?: string;\n properties?: Record<string, unknown> & {\n className?: string[] | string;\n raw?: string;\n };\n data?: Record<string, unknown>;\n children?: Node[];\n raw?: string;\n language?: string;\n codeTitle?: string;\n}\n\nexport function cn(...inputs: ClassValue[]) {\n return twMerge(clsx(inputs));\n}\n\n/** Parse both `dd-MM-yyyy` and ISO 8601 date strings into a Date object. */\nexport function parseDate(dateStr: string): Date {\n if (/^\\d{4}-/.test(dateStr)) return new Date(dateStr);\n const [day, month, year] = dateStr.split(\"-\").map(Number);\n return new Date(year, month - 1, day);\n}\n\nexport function stringToDate(date: string | Date) {\n return date instanceof Date ? date : parseDate(date);\n}\n\n/** Format date to long format (e.g. \"Thursday, April 5, 2026\") */\nexport function formatDate(dateStrOrDate: string | Date): string {\n const date = stringToDate(dateStrOrDate);\n return date.toLocaleDateString(\"en-US\", {\n weekday: \"long\",\n year: \"numeric\",\n month: \"long\",\n day: \"numeric\",\n });\n}\n\n/** Format date to short format (e.g. \"Apr 5, 2026\") */\nexport function formatDate2(dateStrOrDate: string | Date): string {\n const date = stringToDate(dateStrOrDate);\n return date.toLocaleDateString(\"en-US\", {\n month: \"short\",\n day: \"numeric\",\n year: \"numeric\",\n });\n}\n\nexport function toIsoDateOnly(dateStrOrDate: string | Date): string {\n const date = stringToDate(dateStrOrDate);\n return date.toISOString().slice(0, 10);\n}\n"],"mappings":";AAAA,SAA0B,YAAY;AACtC,SAAS,eAAe;AAiBjB,SAAS,MAAM,QAAsB;AAC1C,SAAO,QAAQ,KAAK,MAAM,CAAC;AAC7B;AAGO,SAAS,UAAU,SAAuB;AAC/C,MAAI,UAAU,KAAK,OAAO,EAAG,QAAO,IAAI,KAAK,OAAO;AACpD,QAAM,CAAC,KAAK,OAAO,IAAI,IAAI,QAAQ,MAAM,GAAG,EAAE,IAAI,MAAM;AACxD,SAAO,IAAI,KAAK,MAAM,QAAQ,GAAG,GAAG;AACtC;AAEO,SAAS,aAAa,MAAqB;AAChD,SAAO,gBAAgB,OAAO,OAAO,UAAU,IAAI;AACrD;AAGO,SAAS,WAAW,eAAsC;AAC/D,QAAM,OAAO,aAAa,aAAa;AACvC,SAAO,KAAK,mBAAmB,SAAS;AAAA,IACtC,SAAS;AAAA,IACT,MAAM;AAAA,IACN,OAAO;AAAA,IACP,KAAK;AAAA,EACP,CAAC;AACH;AAGO,SAAS,YAAY,eAAsC;AAChE,QAAM,OAAO,aAAa,aAAa;AACvC,SAAO,KAAK,mBAAmB,SAAS;AAAA,IACtC,OAAO;AAAA,IACP,KAAK;AAAA,IACL,MAAM;AAAA,EACR,CAAC;AACH;AAEO,SAAS,cAAc,eAAsC;AAClE,QAAM,OAAO,aAAa,aAAa;AACvC,SAAO,KAAK,YAAY,EAAE,MAAM,GAAG,EAAE;AACvC;","names":[]}
package/dist/index.d.ts CHANGED
@@ -1,10 +1,118 @@
1
- export type { TocItem, MdxCompileResult } from "./types";
2
- export { cn, parseDate, stringToDate, formatDate, formatDate2, toIsoDateOnly } from "./utils";
3
- export { parseMdx, serialize, MDXRemote, preProcess, postProcess, createDefaultRehypePlugins, createDefaultRemarkPlugins, } from "./compile";
4
- export { handleCodeTitles } from "./plugins/handleCodeTitles";
5
- export { handleCodeExpandableRemark, handleCodeExpandable } from "./plugins/handleCodeExpandable";
6
- export { extractFrontmatter, extractFrontmatterWithContent, extractTocsFromRawMdx, sluggify, } from "./extract";
7
- export type { ParseMdxOptions } from "./compile";
8
- export { readMdxFileBySlug, parseMdxFile, compileParsedMdxFile, createMdxContentService, } from "./content";
9
- export type { ReadMdxFileResult, ParsedMdxFile, CompiledMdxFile, CreateMdxContentServiceOptions, } from "./content";
10
- //# sourceMappingURL=index.d.ts.map
1
+ import { ReactNode } from 'react';
2
+ export { cn, formatDate, formatDate2, parseDate, stringToDate, toIsoDateOnly } from './utils.js';
3
+ import { compileMDX } from 'next-mdx-remote/rsc';
4
+ import { Node } from 'unist';
5
+ import { Pluggable } from 'unified';
6
+ export { MDXRemote } from 'next-mdx-remote';
7
+ export { serialize } from 'next-mdx-remote/serialize';
8
+ import 'clsx';
9
+
10
+ type TocItem = {
11
+ level: number;
12
+ text: string;
13
+ href: string;
14
+ };
15
+ type MdxCompileResult<Frontmatter> = {
16
+ content: ReactNode;
17
+ frontmatter: Frontmatter;
18
+ scope?: Record<string, unknown>;
19
+ };
20
+
21
+ type CompileMdxInput = Parameters<typeof compileMDX<Record<string, unknown>>>[0];
22
+ type CompileMdxOptions = NonNullable<CompileMdxInput["options"]>;
23
+ type CompilerMdxOptions = NonNullable<CompileMdxOptions["mdxOptions"]>;
24
+ type ParseMdxOptions = {
25
+ components?: CompileMdxInput["components"];
26
+ rehypePlugins?: CompilerMdxOptions["rehypePlugins"];
27
+ remarkPlugins?: CompilerMdxOptions["remarkPlugins"];
28
+ /**
29
+ * Whether to parse frontmatter during MDX compilation.
30
+ * Set to `false` when frontmatter is already extracted separately
31
+ * (e.g. via gray-matter) to avoid redundant parsing.
32
+ * Defaults to `true`.
33
+ */
34
+ parseFrontmatter?: boolean;
35
+ };
36
+ declare const preProcess: () => (tree: Node) => Node;
37
+ declare const postProcess: () => (tree: Node) => Node;
38
+ declare function createDefaultRehypePlugins(): Pluggable[];
39
+ declare function createDefaultRemarkPlugins(): Pluggable[];
40
+ declare function parseMdx<Frontmatter>(rawMdx: string, options?: ParseMdxOptions): Promise<MdxCompileResult<Frontmatter>>;
41
+
42
+ declare const handleCodeTitles: () => (tree: Node) => void;
43
+
44
+ declare const handleCodeExpandableRemark: () => (tree: Node) => void;
45
+ declare const handleCodeExpandable: () => (tree: Node) => void;
46
+
47
+ /**
48
+ * Rehype plugin that transforms `<pre><code class="language-mermaid">` fenced
49
+ * blocks into `<Mermaid chart="...">` elements.
50
+ *
51
+ * This allows Mermaid diagram definitions to be authored via standard fenced
52
+ * code blocks (````mermaid) which avoids JSX parsing collisions with
53
+ * Mermaid's `{...}` (decision nodes) and `[...]` (label nodes) syntax.
54
+ */
55
+ declare const rehypeMermaid: () => (tree: Node) => void;
56
+
57
+ declare function sluggify(text: string): string;
58
+ declare function extractTocsFromRawMdx(rawMdx: string): TocItem[];
59
+ declare function extractFrontmatter<Frontmatter>(content: string): Frontmatter;
60
+ /**
61
+ * Extract frontmatter and return both the parsed data and the content
62
+ * with the frontmatter block stripped. Avoids a second parse by compileMDX.
63
+ */
64
+ declare function extractFrontmatterWithContent<Frontmatter>(content: string): {
65
+ frontmatter: Frontmatter;
66
+ strippedContent: string;
67
+ };
68
+
69
+ type CacheFn = <T extends (...args: any[]) => any>(fn: T) => T;
70
+ type ReadMdxFileResult = {
71
+ content: string;
72
+ /** Relative path used for UI links (e.g. "docs/getting-started/index.mdx"). */
73
+ filePath: string;
74
+ /** Absolute path on disk — available when file was read by readMdxFileBySlug. */
75
+ absoluteFilePath?: string;
76
+ };
77
+ type ParsedMdxFile<Frontmatter, T extends TocItem = TocItem> = {
78
+ /** Raw content with frontmatter block stripped — ready to pass to compileMDX. */
79
+ content: string;
80
+ filePath: string;
81
+ frontmatter: Frontmatter;
82
+ tocs: T[];
83
+ };
84
+ type CompiledMdxFile<Frontmatter, T extends TocItem = TocItem> = MdxCompileResult<Frontmatter> & {
85
+ filePath: string;
86
+ tocs: T[];
87
+ };
88
+ type ReadMdxBySlugOptions = {
89
+ rootDir?: string;
90
+ docsDir?: string;
91
+ };
92
+ declare function readMdxFileBySlug(slug: string, options?: ReadMdxBySlugOptions): Promise<ReadMdxFileResult>;
93
+ type ParseMdxFileOptions<T extends TocItem> = {
94
+ tocsExtractor?: (rawMdx: string) => T[];
95
+ };
96
+ declare function parseMdxFile<Frontmatter, T extends TocItem = TocItem>(raw: ReadMdxFileResult, options?: ParseMdxFileOptions<T>): ParsedMdxFile<Frontmatter, T>;
97
+ declare function compileParsedMdxFile<Frontmatter, T extends TocItem = TocItem>(parsed: ParsedMdxFile<Frontmatter, T>, options?: ParseMdxOptions): Promise<CompiledMdxFile<Frontmatter, T>>;
98
+ type CreateMdxContentServiceOptions<Frontmatter, T extends TocItem = TocItem> = {
99
+ parseOptions?: ParseMdxOptions;
100
+ readOptions?: ReadMdxBySlugOptions;
101
+ tocsExtractor?: (rawMdx: string) => T[];
102
+ cacheFn?: CacheFn;
103
+ /**
104
+ * Optional hook to enrich or transform frontmatter after parsing.
105
+ * Called with the parsed frontmatter and the absolute file path.
106
+ * Runs at build time during static generation — ideal for injecting
107
+ * fallback values (e.g. git last-modified date when `date` is absent).
108
+ */
109
+ frontmatterEnricher?: (frontmatter: Frontmatter, absoluteFilePath: string) => Frontmatter | Promise<Frontmatter>;
110
+ };
111
+ declare function createMdxContentService<Frontmatter, T extends TocItem = TocItem>(options?: CreateMdxContentServiceOptions<Frontmatter, T>): {
112
+ getParsedForSlug: (slug: string) => Promise<ParsedMdxFile<Frontmatter, T>>;
113
+ getCompiledForSlug: (slug: string) => Promise<CompiledMdxFile<Frontmatter, T>>;
114
+ getFrontmatterForSlug: (slug: string) => Promise<Frontmatter>;
115
+ getTocsForSlug: (slug: string) => Promise<T[]>;
116
+ };
117
+
118
+ export { type CompiledMdxFile, type CreateMdxContentServiceOptions, type MdxCompileResult, type ParseMdxOptions, type ParsedMdxFile, type ReadMdxFileResult, type TocItem, compileParsedMdxFile, createDefaultRehypePlugins, createDefaultRemarkPlugins, createMdxContentService, extractFrontmatter, extractFrontmatterWithContent, extractTocsFromRawMdx, handleCodeExpandable, handleCodeExpandableRemark, handleCodeTitles, parseMdx, parseMdxFile, postProcess, preProcess, readMdxFileBySlug, rehypeMermaid, sluggify };