@shivaedev/work-board 0.6.0 → 0.6.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.
- package/CHANGELOG.md +18 -0
- package/README.md +8 -0
- package/dist/cli.js +1 -1
- package/dist/page/diagrams.d.ts +1 -1
- package/dist/page/diagrams.d.ts.map +1 -1
- package/dist/page/diagrams.js +1 -0
- package/dist/page/diagrams.js.map +1 -1
- package/dist/render/code.d.ts +2 -8
- package/dist/render/code.d.ts.map +1 -1
- package/dist/render/code.js +26 -18
- package/dist/render/code.js.map +1 -1
- package/dist/render/documentHeadings.d.ts +2 -658
- package/dist/render/documentHeadings.d.ts.map +1 -1
- package/dist/render/documentHeadings.js +20 -23
- package/dist/render/documentHeadings.js.map +1 -1
- package/dist/render/documentLinks.d.ts +2 -659
- package/dist/render/documentLinks.d.ts.map +1 -1
- package/dist/render/documentLinks.js +22 -25
- package/dist/render/documentLinks.js.map +1 -1
- package/dist/render/footnotes.d.ts +2 -18
- package/dist/render/footnotes.d.ts.map +1 -1
- package/dist/render/footnotes.js +9 -11
- package/dist/render/footnotes.js.map +1 -1
- package/dist/render/markdown.d.ts.map +1 -1
- package/dist/render/markdown.js +13 -4
- package/dist/render/markdown.js.map +1 -1
- package/dist/render/textContent.d.ts +3 -0
- package/dist/render/textContent.d.ts.map +1 -0
- package/dist/render/textContent.js +7 -0
- package/dist/render/textContent.js.map +1 -0
- package/dist/search/collector.d.ts +2 -8
- package/dist/search/collector.d.ts.map +1 -1
- package/dist/search/collector.js +29 -25
- package/dist/search/collector.js.map +1 -1
- package/dist/search/entries.d.ts.map +1 -1
- package/dist/search/entries.js +7 -3
- package/dist/search/entries.js.map +1 -1
- package/dist/search/linkCollector.d.ts +2 -7
- package/dist/search/linkCollector.d.ts.map +1 -1
- package/dist/search/linkCollector.js +12 -13
- package/dist/search/linkCollector.js.map +1 -1
- package/package.json +10 -1
- package/src/cli.ts +1 -1
- package/src/page/diagrams.ts +1 -0
- package/src/render/code.ts +29 -19
- package/src/render/documentHeadings.ts +21 -23
- package/src/render/documentLinks.ts +23 -25
- package/src/render/footnotes.ts +11 -14
- package/src/render/markdown.ts +17 -4
- package/src/render/textContent.ts +8 -0
- package/src/search/collector.ts +30 -27
- package/src/search/entries.ts +7 -3
- package/src/search/linkCollector.ts +13 -13
|
@@ -1,30 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Element, Root } from "hast";
|
|
2
|
+
import { visit as visitElements } from "unist-util-visit";
|
|
2
3
|
import { fileUrl } from "#files/url.ts";
|
|
3
4
|
import { attachmentHref } from "#path/attachment.ts";
|
|
4
5
|
|
|
5
6
|
export function documentLinks(file: string) {
|
|
6
|
-
|
|
7
|
-
element:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
},
|
|
28
|
-
name: "work-board-document-links",
|
|
29
|
-
});
|
|
7
|
+
function resolve(element: Element) {
|
|
8
|
+
const attribute = element.tagName === "img" ? "src" : "href";
|
|
9
|
+
const href = element.properties[attribute];
|
|
10
|
+
if (typeof href !== "string" || href.startsWith("#") || href.startsWith("//") || /^[a-z][a-z\d+.-]*:/iu.exec(href) !== null) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const target = new URL(href, `http://work-board.local${fileUrl(file)}`);
|
|
14
|
+
const resolved = target.pathname + target.search + target.hash;
|
|
15
|
+
const image = attachmentHref(resolved);
|
|
16
|
+
element.properties[attribute] = image ?? resolved;
|
|
17
|
+
if (element.tagName === "img" && image) {
|
|
18
|
+
element.properties.dataLocalImage = image;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return () => (tree: Root) => {
|
|
22
|
+
visitElements(tree, "element", (element) => {
|
|
23
|
+
if (element.tagName === "a" || element.tagName === "img") {
|
|
24
|
+
resolve(element);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
};
|
|
30
28
|
}
|
package/src/render/footnotes.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Element } from "hast";
|
|
2
|
-
import {
|
|
1
|
+
import type { Element, Root } from "hast";
|
|
2
|
+
import { visit as visitElements } from "unist-util-visit";
|
|
3
3
|
|
|
4
4
|
const FOOTNOTE_IDS = /^(?:user-content-fn|footnote-label)/u;
|
|
5
5
|
|
|
@@ -17,16 +17,13 @@ const scoped = (prefix: string, value: Value): Value => {
|
|
|
17
17
|
return Array.isArray(value) ? value.map((each) => (typeof each === "string" ? String(scoped(prefix, each)) : each)) : value;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
-
export const footnoteIds = (prefix: string) =>
|
|
21
|
-
|
|
22
|
-
element
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
),
|
|
29
|
-
}),
|
|
30
|
-
},
|
|
31
|
-
name: "work-board-footnote-ids",
|
|
20
|
+
export const footnoteIds = (prefix: string) => () => (tree: Root) => {
|
|
21
|
+
visitElements(tree, "element", (element) => {
|
|
22
|
+
if (!["a", "h2", "li"].includes(element.tagName)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
element.properties = Object.fromEntries(
|
|
26
|
+
Object.entries(element.properties).map(([name, value]) => [name, SCOPED.has(name) ? scoped(prefix, value) : value]),
|
|
27
|
+
);
|
|
32
28
|
});
|
|
29
|
+
};
|
package/src/render/markdown.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import {
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import { renderToStaticMarkup } from "react-dom/server";
|
|
4
|
+
import { MarkdownAsync } from "react-markdown";
|
|
5
|
+
import rehypeRaw from "rehype-raw";
|
|
6
|
+
import remarkGfm from "remark-gfm";
|
|
7
|
+
import type { PluggableList } from "unified";
|
|
3
8
|
import { codeBlocks } from "./code.ts";
|
|
4
9
|
import type { documentHeadings } from "./documentHeadings.ts";
|
|
5
10
|
import { documentLinks } from "./documentLinks.ts";
|
|
@@ -15,7 +20,7 @@ export interface RenderOptions {
|
|
|
15
20
|
|
|
16
21
|
export const renderMarkdown = Effect.fn("WorkBoard.renderMarkdown")(function* (source: string, options: RenderOptions = {}) {
|
|
17
22
|
const highlighter = yield* Highlighter;
|
|
18
|
-
const plugins:
|
|
23
|
+
const plugins: PluggableList = [rehypeRaw, codeBlocks(highlighter)];
|
|
19
24
|
if (options.idPrefix !== undefined) {
|
|
20
25
|
plugins.push(footnoteIds(options.idPrefix));
|
|
21
26
|
}
|
|
@@ -25,9 +30,17 @@ export const renderMarkdown = Effect.fn("WorkBoard.renderMarkdown")(function* (s
|
|
|
25
30
|
if (options.headings !== undefined) {
|
|
26
31
|
plugins.push(options.headings.plugin);
|
|
27
32
|
}
|
|
28
|
-
const
|
|
33
|
+
const html = yield* Effect.tryPromise({
|
|
29
34
|
catch: (cause) => new RenderFailed({ cause }),
|
|
30
|
-
try: async () =>
|
|
35
|
+
try: async () =>
|
|
36
|
+
renderToStaticMarkup(
|
|
37
|
+
await MarkdownAsync({
|
|
38
|
+
children: source,
|
|
39
|
+
components: { img: ({ node: _node, ...props }) => createElement("img", { ...props, loading: "lazy" }) },
|
|
40
|
+
rehypePlugins: plugins,
|
|
41
|
+
remarkPlugins: [remarkGfm],
|
|
42
|
+
}),
|
|
43
|
+
),
|
|
31
44
|
});
|
|
32
45
|
return html;
|
|
33
46
|
});
|
package/src/search/collector.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { Element } from "hast";
|
|
2
|
-
import {
|
|
1
|
+
import type { Element, Root } from "hast";
|
|
2
|
+
import { visit as visitElements } from "unist-util-visit";
|
|
3
3
|
import { fileUrl } from "#files/url.ts";
|
|
4
|
+
import { textContent } from "#render/textContent.ts";
|
|
4
5
|
import type { Entry } from "./entries.ts";
|
|
5
6
|
|
|
6
7
|
function sourceLine(element: Element, first: number | undefined, length: number): { readonly line?: number } {
|
|
@@ -14,31 +15,33 @@ export function searchCollector(file: string, startLine: () => number | undefine
|
|
|
14
15
|
const entries: Entry[] = [];
|
|
15
16
|
let passage = "";
|
|
16
17
|
let label = file;
|
|
17
|
-
|
|
18
|
-
element
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
function collect(element: Element) {
|
|
19
|
+
const text = element.children.map(textContent).join("").replace(/\s+/gu, " ").trim();
|
|
20
|
+
if (!text || element.properties.id === "footnote-label") {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const heading = element.tagName.startsWith("h") && element.tagName !== "th";
|
|
24
|
+
if (heading && typeof element.properties.id === "string") {
|
|
25
|
+
passage = element.properties.id;
|
|
26
|
+
label = text;
|
|
27
|
+
}
|
|
28
|
+
entries.push({
|
|
29
|
+
file,
|
|
30
|
+
href: fileUrl(file) + (passage ? `#${encodeURIComponent(passage)}` : ""),
|
|
31
|
+
kind: heading ? "heading" : "passage",
|
|
32
|
+
...sourceLine(element, startLine(), length()),
|
|
33
|
+
text,
|
|
34
|
+
title: label,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function plugin() {
|
|
38
|
+
return function transform(tree: Root) {
|
|
39
|
+
visitElements(tree, "element", (element) => {
|
|
40
|
+
if (["h1", "h2", "h3", "h4", "h5", "h6", "p", "pre", "li", "td", "th"].includes(element.tagName)) {
|
|
41
|
+
collect(element);
|
|
29
42
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
kind: heading ? "heading" : "passage",
|
|
34
|
-
...sourceLine(element, startLine(), length()),
|
|
35
|
-
text,
|
|
36
|
-
title: label,
|
|
37
|
-
});
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
name: "work-board-search-text",
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
}
|
|
43
46
|
return { entries, plugin };
|
|
44
47
|
}
|
package/src/search/entries.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MarkdownAsync } from "react-markdown";
|
|
2
|
+
import rehypeRaw from "rehype-raw";
|
|
3
|
+
import remarkGfm from "remark-gfm";
|
|
2
4
|
import { fileUrl } from "#files/url.ts";
|
|
3
5
|
import { boardOf } from "#render/board.ts";
|
|
4
6
|
import { documentHeadings } from "#render/documentHeadings.ts";
|
|
@@ -41,8 +43,10 @@ export async function entriesOf(source: string, file: string, home: boolean, bod
|
|
|
41
43
|
if (fragment) {
|
|
42
44
|
fragmentLength = fragment.split("\n").length;
|
|
43
45
|
fragmentLine = board ? fragmentLocation(source, fragment, bodyLine, seenFragments) : bodyLine;
|
|
44
|
-
await
|
|
45
|
-
|
|
46
|
+
await MarkdownAsync({
|
|
47
|
+
children: board?.definitions ? `${fragment}\n\n${board.definitions}` : fragment,
|
|
48
|
+
rehypePlugins: [rehypeRaw, headings.plugin, plugin, linkCollector(links)],
|
|
49
|
+
remarkPlugins: [remarkGfm],
|
|
46
50
|
});
|
|
47
51
|
}
|
|
48
52
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Root } from "hast";
|
|
2
|
+
import { visit as visitElements } from "unist-util-visit";
|
|
2
3
|
|
|
3
4
|
export function linkCollector(links: Set<string>) {
|
|
4
|
-
return
|
|
5
|
-
element
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
});
|
|
5
|
+
return () => (tree: Root) => {
|
|
6
|
+
visitElements(tree, "element", (element) => {
|
|
7
|
+
if (element.tagName !== "a") {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const href = element.properties.href;
|
|
11
|
+
if (typeof href === "string" && !href.startsWith("#")) {
|
|
12
|
+
links.add(href);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
16
|
}
|