@docspack/sheaf-astro 0.3.0 → 0.4.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.
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +22 -6
- package/dist/render.js.map +1 -1
- package/package.json +2 -2
- package/src/render.ts +23 -6
package/dist/render.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,aAAa,EAElB,KAAK,WAAW,EACjB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,OAAO,EACZ,KAAK,WAAW,EAEhB,KAAK,IAAI,EACV,MAAM,iBAAiB,CAAC;AAEzB;;;;;;;;;;;;GAYG;AAEH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;;;;OAMG;IACH,QAAQ,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,CAAC;CACvC;AAED,mDAAmD;AACnD,MAAM,WAAW,eAAe;IAC9B;;;;;;OAMG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,KAAK,GAAG,OAAO,GAAG,OAAO,CAAC;IACrD,6FAA6F;IAC7F,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC;IACnC,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;;;;;;;;;OAaG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,aAAa,CAAC;IACvC;;;;;;;;;OASG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,aAAa,CAAC;IACvC;;;;;;OAMG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,WAAW,GAAG;QAAE,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAA;KAAE,CAAC;CACjE;
|
|
1
|
+
{"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,GAAE,eAAoB,GAAG,cAAc,CA+BpF;AAED,iDAAiD;AACjD,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,eAAoB,GAAG,YAAY,CAK9E"}
|
package/dist/render.js
CHANGED
|
@@ -4,12 +4,19 @@ import { linkHref, } from "@docspack/sheaf";
|
|
|
4
4
|
* The remark side of `links`. The processor is shared by every page, so the page being rendered
|
|
5
5
|
* arrives the way Astro hands any document's metadata to a plugin: in its frontmatter, where
|
|
6
6
|
* `render` puts the page's slug.
|
|
7
|
+
*
|
|
8
|
+
* A broken link is recorded against that frontmatter object rather than thrown. Astro catches
|
|
9
|
+
* anything a plugin throws, logs it and rethrows it prefixed `Failed to parse Markdown file
|
|
10
|
+
* "undefined":` — a page here has no file URL, and giving it one would switch on Astro's image
|
|
11
|
+
* collection, which rewrites every `<img>`. `render` throws the recorded error once Astro returns,
|
|
12
|
+
* with the message `linkHref` wrote.
|
|
7
13
|
*/
|
|
8
|
-
function rewritePageLinks(links) {
|
|
14
|
+
function rewritePageLinks(links, failures) {
|
|
9
15
|
return () => (tree, file) => {
|
|
10
|
-
const
|
|
16
|
+
const frontmatter = file.data.astro?.frontmatter;
|
|
17
|
+
const slug = frontmatter?.slug;
|
|
11
18
|
const from = links.graph.pages.find((page) => page.slug === slug);
|
|
12
|
-
if (from === undefined)
|
|
19
|
+
if (from === undefined || frontmatter === undefined)
|
|
13
20
|
return;
|
|
14
21
|
const visit = (node) => {
|
|
15
22
|
if ((node.type === "link" || node.type === "definition") && node.url) {
|
|
@@ -18,7 +25,12 @@ function rewritePageLinks(links) {
|
|
|
18
25
|
for (const child of node.children ?? [])
|
|
19
26
|
visit(child);
|
|
20
27
|
};
|
|
21
|
-
|
|
28
|
+
try {
|
|
29
|
+
visit(tree);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
failures.set(frontmatter, error);
|
|
33
|
+
}
|
|
22
34
|
};
|
|
23
35
|
}
|
|
24
36
|
/**
|
|
@@ -34,6 +46,7 @@ function rewritePageLinks(links) {
|
|
|
34
46
|
*/
|
|
35
47
|
export function createMarkdownRenderer(options = {}) {
|
|
36
48
|
let pending;
|
|
49
|
+
const failures = new WeakMap();
|
|
37
50
|
const renderer = () => {
|
|
38
51
|
pending ??= createMarkdownProcessor({
|
|
39
52
|
gfm: options.gfm ?? true,
|
|
@@ -42,7 +55,7 @@ export function createMarkdownRenderer(options = {}) {
|
|
|
42
55
|
: { syntaxHighlight: options.syntaxHighlight }),
|
|
43
56
|
remarkPlugins: [
|
|
44
57
|
...(options.remarkPlugins ?? []),
|
|
45
|
-
...(options.links === undefined ? [] : [rewritePageLinks(options.links)]),
|
|
58
|
+
...(options.links === undefined ? [] : [rewritePageLinks(options.links, failures)]),
|
|
46
59
|
],
|
|
47
60
|
...(options.rehypePlugins === undefined
|
|
48
61
|
? {}
|
|
@@ -52,7 +65,10 @@ export function createMarkdownRenderer(options = {}) {
|
|
|
52
65
|
return pending;
|
|
53
66
|
};
|
|
54
67
|
return async (markdown, page) => {
|
|
55
|
-
const
|
|
68
|
+
const frontmatter = page === undefined ? undefined : { ...page.frontmatter, slug: page.slug };
|
|
69
|
+
const { code, metadata } = await (await renderer()).render(markdown, frontmatter === undefined ? undefined : { frontmatter });
|
|
70
|
+
if (frontmatter !== undefined && failures.has(frontmatter))
|
|
71
|
+
throw failures.get(frontmatter);
|
|
56
72
|
return { html: code, headings: metadata.headings };
|
|
57
73
|
};
|
|
58
74
|
}
|
package/dist/render.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render.js","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EAIvB,gBAAgB,GAEjB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAIL,QAAQ,GAET,MAAM,iBAAiB,CAAC;AAuFzB
|
|
1
|
+
{"version":3,"file":"render.js","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EA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,EAAE,GAAG,MAAM,CAAC,MAAM,QAAQ,EAAE,CAAC,CAAC,MAAM,CACxD,QAAQ,EACR,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CACxD,CAAC;QACF,IAAI,WAAW,KAAK,SAAS,IAAI,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC;YAAE,MAAM,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QAC5F,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,CAAC,QAAQ,EAAE,CAAC;IACrD,CAAC,CAAC;AACJ,CAAC;AAED,iDAAiD;AACjD,MAAM,UAAU,kBAAkB,CAAC,UAA2B,EAAE;IAC9D,MAAM,MAAM,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAC;IAC/C,kGAAkG;IAClG,8DAA8D;IAC9D,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAC9C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docspack/sheaf-astro",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Renders a Sheaf content graph with Astro's own Markdown pipeline, so a docs site and its agent index come from one parse.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@astrojs/markdown-remark": "7.2.0",
|
|
31
|
-
"@docspack/sheaf": "^0.
|
|
31
|
+
"@docspack/sheaf": "^0.4.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@docspack/config": "0.1.0",
|
package/src/render.ts
CHANGED
|
@@ -103,12 +103,22 @@ interface AstroFile {
|
|
|
103
103
|
* The remark side of `links`. The processor is shared by every page, so the page being rendered
|
|
104
104
|
* arrives the way Astro hands any document's metadata to a plugin: in its frontmatter, where
|
|
105
105
|
* `render` puts the page's slug.
|
|
106
|
+
*
|
|
107
|
+
* A broken link is recorded against that frontmatter object rather than thrown. Astro catches
|
|
108
|
+
* anything a plugin throws, logs it and rethrows it prefixed `Failed to parse Markdown file
|
|
109
|
+
* "undefined":` — a page here has no file URL, and giving it one would switch on Astro's image
|
|
110
|
+
* collection, which rewrites every `<img>`. `render` throws the recorded error once Astro returns,
|
|
111
|
+
* with the message `linkHref` wrote.
|
|
106
112
|
*/
|
|
107
|
-
function rewritePageLinks(
|
|
113
|
+
function rewritePageLinks(
|
|
114
|
+
links: NonNullable<RendererOptions["links"]>,
|
|
115
|
+
failures: WeakMap<object, unknown>,
|
|
116
|
+
) {
|
|
108
117
|
return () => (tree: MdastNode, file: AstroFile) => {
|
|
109
|
-
const
|
|
118
|
+
const frontmatter = file.data.astro?.frontmatter;
|
|
119
|
+
const slug = frontmatter?.slug;
|
|
110
120
|
const from = links.graph.pages.find((page) => page.slug === slug);
|
|
111
|
-
if (from === undefined) return;
|
|
121
|
+
if (from === undefined || frontmatter === undefined) return;
|
|
112
122
|
|
|
113
123
|
const visit = (node: MdastNode) => {
|
|
114
124
|
if ((node.type === "link" || node.type === "definition") && node.url) {
|
|
@@ -116,7 +126,11 @@ function rewritePageLinks(links: NonNullable<RendererOptions["links"]>) {
|
|
|
116
126
|
}
|
|
117
127
|
for (const child of node.children ?? []) visit(child);
|
|
118
128
|
};
|
|
119
|
-
|
|
129
|
+
try {
|
|
130
|
+
visit(tree);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
failures.set(frontmatter, error);
|
|
133
|
+
}
|
|
120
134
|
};
|
|
121
135
|
}
|
|
122
136
|
|
|
@@ -141,6 +155,7 @@ export type SourceRenderer = (markdown: string, page?: Page) => Promise<Rendered
|
|
|
141
155
|
*/
|
|
142
156
|
export function createMarkdownRenderer(options: RendererOptions = {}): SourceRenderer {
|
|
143
157
|
let pending: Promise<MarkdownRenderer> | undefined;
|
|
158
|
+
const failures = new WeakMap<object, unknown>();
|
|
144
159
|
|
|
145
160
|
const renderer = (): Promise<MarkdownRenderer> => {
|
|
146
161
|
pending ??= createMarkdownProcessor({
|
|
@@ -150,7 +165,7 @@ export function createMarkdownRenderer(options: RendererOptions = {}): SourceRen
|
|
|
150
165
|
: { syntaxHighlight: options.syntaxHighlight }),
|
|
151
166
|
remarkPlugins: [
|
|
152
167
|
...(options.remarkPlugins ?? []),
|
|
153
|
-
...(options.links === undefined ? [] : [rewritePageLinks(options.links)]),
|
|
168
|
+
...(options.links === undefined ? [] : [rewritePageLinks(options.links, failures)]),
|
|
154
169
|
],
|
|
155
170
|
...(options.rehypePlugins === undefined
|
|
156
171
|
? {}
|
|
@@ -161,10 +176,12 @@ export function createMarkdownRenderer(options: RendererOptions = {}): SourceRen
|
|
|
161
176
|
};
|
|
162
177
|
|
|
163
178
|
return async (markdown, page) => {
|
|
179
|
+
const frontmatter = page === undefined ? undefined : { ...page.frontmatter, slug: page.slug };
|
|
164
180
|
const { code, metadata } = await (await renderer()).render(
|
|
165
181
|
markdown,
|
|
166
|
-
|
|
182
|
+
frontmatter === undefined ? undefined : { frontmatter },
|
|
167
183
|
);
|
|
184
|
+
if (frontmatter !== undefined && failures.has(frontmatter)) throw failures.get(frontmatter);
|
|
168
185
|
return { html: code, headings: metadata.headings };
|
|
169
186
|
};
|
|
170
187
|
}
|