Skip to content

Commit 2ab94ae

Browse files
committed
fix pagelist snippets not parsed correctly
1 parent f6c1d08 commit 2ab94ae

File tree

8 files changed

+206
-142
lines changed

8 files changed

+206
-142
lines changed

.changeset/quiet-pans-burn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@hyperbook/markdown": patch
3+
"hyperbook": patch
4+
---
5+
6+
fix pagelist snippets not parsed correctly

packages/markdown/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"mdast-util-from-markdown": "^2.0.1",
4949
"mdast-util-to-hast": "^13.1.0",
5050
"mdast-util-to-string": "^4.0.0",
51+
"micromark-extension-directive": "^3.0.2",
5152
"pako": "2.1.0",
5253
"qrcode-svg": "^1.1.0",
5354
"rehype-format": "^5.0.1",

packages/markdown/src/process.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import remarkLink from "./remarkLink";
4747
import remarkDirectivePagelist from "./remarkDirectivePagelist";
4848
import rehypeQrCode from "./rehypeQrCode";
4949

50-
const remark = (ctx: HyperbookContext) => {
50+
export const remark = (ctx: HyperbookContext) => {
5151
const remarkPlugins: PluggableList = [
5252
remarkRemoveComments,
5353
remarkDirective,
@@ -116,15 +116,16 @@ const remark = (ctx: HyperbookContext) => {
116116
.use(remarkToRehype, {
117117
allowDangerousHtml: ctx.config.allowDangerousHtml || false,
118118
})
119-
.use(rehypePlugins)
119+
.use(rehypePlugins);
120+
};
121+
122+
export const process = (md: string, ctx: HyperbookContext) => {
123+
return remark(ctx)
120124
.use(rehypeShell(ctx))
121125
.use(rehypeHtmlStructure(ctx))
122126
.use(rehypeStringify, {
123127
allowDangerousCharacters: true,
124128
allowDangerousHtml: ctx.config.allowDangerousHtml || false,
125-
});
126-
};
127-
128-
export const process = (md: string, ctx: HyperbookContext) => {
129-
return remark(ctx).process(md);
129+
})
130+
.process(md);
130131
};

packages/markdown/src/remarkDirectivePagelist.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,16 @@ import {
88
} from "@hyperbook/types";
99
import handlebars from "handlebars";
1010
import fs from "fs";
11-
import { Root } from "mdast";
12-
import { fromMarkdown } from "mdast-util-from-markdown";
11+
import path from "path";
12+
import { Node, Root } from "mdast";
1313
import { visit } from "unist-util-visit";
1414
import { VFile } from "vfile";
15-
import { Element } from "hast";
1615
import {
1716
expectLeafDirective,
1817
isDirective,
1918
registerDirective,
2019
} from "./remarkHelper";
21-
import path from "path";
22-
import { toHast } from "mdast-util-to-hast";
23-
import { type } from "os";
24-
import { table } from "console";
20+
import { remark } from "./process";
2521

2622
const getPageList = (
2723
sections: HyperbookSection[],
@@ -141,12 +137,8 @@ export default (ctx: HyperbookContext) => () => {
141137
);
142138
const template = handlebars.compile(snippetFile);
143139
const content = template({ pages: filteredPages });
144-
145-
data.hChildren = [
146-
toHast(fromMarkdown(content), {
147-
allowDangerousHtml: ctx.config.allowDangerousHtml || false,
148-
}) as Element,
149-
];
140+
const contentTree = remark(ctx).parse(content);
141+
node.children = contentTree.children as any;
150142
} else if (format === "ul") {
151143
data.hChildren = [
152144
{

0 commit comments

Comments
 (0)