@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.
Files changed (53) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +8 -0
  3. package/dist/cli.js +1 -1
  4. package/dist/page/diagrams.d.ts +1 -1
  5. package/dist/page/diagrams.d.ts.map +1 -1
  6. package/dist/page/diagrams.js +1 -0
  7. package/dist/page/diagrams.js.map +1 -1
  8. package/dist/render/code.d.ts +2 -8
  9. package/dist/render/code.d.ts.map +1 -1
  10. package/dist/render/code.js +26 -18
  11. package/dist/render/code.js.map +1 -1
  12. package/dist/render/documentHeadings.d.ts +2 -658
  13. package/dist/render/documentHeadings.d.ts.map +1 -1
  14. package/dist/render/documentHeadings.js +20 -23
  15. package/dist/render/documentHeadings.js.map +1 -1
  16. package/dist/render/documentLinks.d.ts +2 -659
  17. package/dist/render/documentLinks.d.ts.map +1 -1
  18. package/dist/render/documentLinks.js +22 -25
  19. package/dist/render/documentLinks.js.map +1 -1
  20. package/dist/render/footnotes.d.ts +2 -18
  21. package/dist/render/footnotes.d.ts.map +1 -1
  22. package/dist/render/footnotes.js +9 -11
  23. package/dist/render/footnotes.js.map +1 -1
  24. package/dist/render/markdown.d.ts.map +1 -1
  25. package/dist/render/markdown.js +13 -4
  26. package/dist/render/markdown.js.map +1 -1
  27. package/dist/render/textContent.d.ts +3 -0
  28. package/dist/render/textContent.d.ts.map +1 -0
  29. package/dist/render/textContent.js +7 -0
  30. package/dist/render/textContent.js.map +1 -0
  31. package/dist/search/collector.d.ts +2 -8
  32. package/dist/search/collector.d.ts.map +1 -1
  33. package/dist/search/collector.js +29 -25
  34. package/dist/search/collector.js.map +1 -1
  35. package/dist/search/entries.d.ts.map +1 -1
  36. package/dist/search/entries.js +7 -3
  37. package/dist/search/entries.js.map +1 -1
  38. package/dist/search/linkCollector.d.ts +2 -7
  39. package/dist/search/linkCollector.d.ts.map +1 -1
  40. package/dist/search/linkCollector.js +12 -13
  41. package/dist/search/linkCollector.js.map +1 -1
  42. package/package.json +10 -1
  43. package/src/cli.ts +1 -1
  44. package/src/page/diagrams.ts +1 -0
  45. package/src/render/code.ts +29 -19
  46. package/src/render/documentHeadings.ts +21 -23
  47. package/src/render/documentLinks.ts +23 -25
  48. package/src/render/footnotes.ts +11 -14
  49. package/src/render/markdown.ts +17 -4
  50. package/src/render/textContent.ts +8 -0
  51. package/src/search/collector.ts +30 -27
  52. package/src/search/entries.ts +7 -3
  53. package/src/search/linkCollector.ts +13 -13
@@ -1,30 +1,28 @@
1
- import { defineHastPlugin } from "satteri";
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
- return defineHastPlugin({
7
- element: {
8
- filter: ["a", "img"],
9
- visit: (element) => {
10
- const attribute = element.tagName === "img" ? "src" : "href";
11
- const href = element.properties[attribute];
12
- if (typeof href !== "string" || href.startsWith("#") || href.startsWith("//") || /^[a-z][a-z\d+.-]*:/iu.exec(href) !== null) {
13
- return;
14
- }
15
- const target = new URL(href, `http://work-board.local${fileUrl(file)}`);
16
- const resolved = target.pathname + target.search + target.hash;
17
- const image = attachmentHref(resolved);
18
- return {
19
- ...element,
20
- properties: {
21
- ...element.properties,
22
- [attribute]: image ?? resolved,
23
- ...(element.tagName === "img" && image ? { dataLocalImage: image } : {}),
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
  }
@@ -1,5 +1,5 @@
1
- import type { Element } from "hast";
2
- import { defineHastPlugin } from "satteri";
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
- defineHastPlugin({
22
- element: {
23
- filter: ["a", "h2", "li"],
24
- visit: (element) => ({
25
- ...element,
26
- properties: Object.fromEntries(
27
- Object.entries(element.properties).map(([name, value]) => [name, SCOPED.has(name) ? scoped(prefix, value) : value]),
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
+ };
@@ -1,5 +1,10 @@
1
1
  import { Effect } from "effect";
2
- import { type HastPluginInput, markdownToHtml } from "satteri";
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: HastPluginInput[] = [codeBlocks(highlighter)];
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 { html } = yield* Effect.tryPromise({
33
+ const html = yield* Effect.tryPromise({
29
34
  catch: (cause) => new RenderFailed({ cause }),
30
- try: async () => markdownToHtml(source, { hastPlugins: plugins }),
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
  });
@@ -0,0 +1,8 @@
1
+ import type { ElementContent } from "hast";
2
+
3
+ export function textContent(node: ElementContent): string {
4
+ if (node.type === "text") {
5
+ return node.value;
6
+ }
7
+ return node.type === "element" ? node.children.map(textContent).join("") : "";
8
+ }
@@ -1,6 +1,7 @@
1
- import type { Element } from "hast";
2
- import { defineHastPlugin } from "satteri";
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
- const plugin = defineHastPlugin({
18
- element: {
19
- filter: ["h1", "h2", "h3", "h4", "h5", "h6", "p", "pre", "li", "td", "th"],
20
- visit: (element, context) => {
21
- const text = context.textContent(element).replace(/\s+/gu, " ").trim();
22
- if (!text || element.properties.id === "footnote-label") {
23
- return;
24
- }
25
- const heading = element.tagName.startsWith("h") && element.tagName !== "th";
26
- if (heading && typeof element.properties.id === "string") {
27
- passage = element.properties.id;
28
- label = text;
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
- entries.push({
31
- file,
32
- href: fileUrl(file) + (passage ? `#${encodeURIComponent(passage)}` : ""),
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
  }
@@ -1,4 +1,6 @@
1
- import { markdownToHtml } from "satteri";
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 markdownToHtml(board?.definitions ? `${fragment}\n\n${board.definitions}` : fragment, {
45
- hastPlugins: [headings.plugin, plugin, linkCollector(links)],
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 { defineHastPlugin } from "satteri";
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 defineHastPlugin({
5
- element: {
6
- filter: ["a"],
7
- visit: (element) => {
8
- const href = element.properties.href;
9
- if (typeof href === "string" && !href.startsWith("#")) {
10
- links.add(href);
11
- }
12
- },
13
- },
14
- name: "work-board-link-collector",
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
  }