reallysimpledocs 1.0.2 → 1.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reallysimpledocs",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.js",
@@ -1,13 +1,16 @@
1
1
  import rehypeParse from "rehype-parse";
2
2
  import rehypeStringify from "rehype-stringify";
3
3
  import { unified } from "unified";
4
+ import GithubSlugger from "github-slugger";
4
5
 
5
6
  const containerClass = ["table-container", "scrollbar", "my-6"];
6
7
  const tableClass = ["table"];
7
8
 
8
9
  export function rehypeBasecoatTables() {
9
10
  return (tree) => {
11
+ transformHeadings(tree);
10
12
  transformTables(tree);
13
+ transformCodeBlocks(tree);
11
14
  };
12
15
  }
13
16
 
@@ -40,6 +43,60 @@ function transformTables(parent) {
40
43
  }
41
44
  }
42
45
 
46
+ function transformHeadings(parent, slugger = new GithubSlugger()) {
47
+ if (!Array.isArray(parent?.children)) return;
48
+
49
+ for (const child of parent.children) {
50
+ if (isHeading(child)) {
51
+ addHeadingAnchor(child, slugger);
52
+ continue;
53
+ }
54
+ transformHeadings(child, slugger);
55
+ }
56
+ }
57
+
58
+ function addHeadingAnchor(node, slugger) {
59
+ if (node.children?.some((child) => isElement(child, "a") && classList(child).includes("header-anchor"))) {
60
+ return;
61
+ }
62
+
63
+ node.properties ||= {};
64
+ const id = String(node.properties.id || "") || slugger.slug(nodeText(node));
65
+ if (!id) return;
66
+ node.properties.id = id;
67
+ node.properties.tabIndex ??= -1;
68
+ node.children = [
69
+ {
70
+ type: "element",
71
+ tagName: "a",
72
+ properties: { className: ["header-anchor"], href: `#${id}` },
73
+ children: node.children || [],
74
+ },
75
+ ];
76
+ }
77
+
78
+ function isHeading(node) {
79
+ return /^h[1-6]$/.test(node?.tagName || "");
80
+ }
81
+
82
+ function transformCodeBlocks(parent) {
83
+ if (!Array.isArray(parent?.children)) return;
84
+
85
+ for (const child of parent.children) {
86
+ if (isElement(child, "pre") && child.children?.some((node) => isElement(node, "code"))) {
87
+ addClass(child, ["scrollbar"]);
88
+ continue;
89
+ }
90
+ transformCodeBlocks(child);
91
+ }
92
+ }
93
+
94
+ function nodeText(node) {
95
+ if (node?.type === "text") return node.value || "";
96
+ if (!Array.isArray(node?.children)) return "";
97
+ return node.children.map(nodeText).join("");
98
+ }
99
+
43
100
  function isElement(node, tagName) {
44
101
  return node?.type === "element" && node.tagName === tagName;
45
102
  }
@@ -12,7 +12,7 @@
12
12
  > h4 {
13
13
  @apply sm:text-lg my-6 scroll-m-18 font-semibold tracking-tight;
14
14
 
15
- a {
15
+ > a.header-anchor {
16
16
  @apply no-underline;
17
17
  }
18
18
  }
@@ -47,7 +47,7 @@
47
47
  @apply block min-w-full w-max px-4 py-3 text-[13px];
48
48
  }
49
49
  pre:has(> code) {
50
- @apply scrollbar max-w-full overflow-x-auto;
50
+ @apply max-w-full overflow-x-auto;
51
51
  }
52
52
  > blockquote {
53
53
  @apply border-l-4 border-border pl-4;
@@ -191,7 +191,7 @@ html.dark .astro-code span {
191
191
  }
192
192
 
193
193
  .code-block > pre {
194
- @apply scrollbar my-0 max-w-full overflow-x-auto rounded-none border-0;
194
+ @apply my-0 max-w-full overflow-x-auto rounded-none border-0;
195
195
  }
196
196
 
197
197
  .code-block > pre > code {
@@ -3,7 +3,7 @@ import { codeToHtml } from "shiki";
3
3
  export async function highlightCode(code, lang = "text") {
4
4
  const value = String(code || "");
5
5
  const language = String(lang || "text").toLowerCase() === "mdx" ? "tsx" : String(lang || "text");
6
- return codeToHtml(value, {
6
+ const html = await codeToHtml(value, {
7
7
  lang: language,
8
8
  themes: {
9
9
  light: "github-light",
@@ -11,4 +11,15 @@ export async function highlightCode(code, lang = "text") {
11
11
  },
12
12
  defaultColor: false,
13
13
  });
14
+ return addPreClass(html, "scrollbar");
15
+ }
16
+
17
+ function addPreClass(html, className) {
18
+ return String(html).replace(/<pre(\s[^>]*)?>/, (match, attrs = "") => {
19
+ const classMatch = attrs.match(/\sclass=(["'])(.*?)\1/s);
20
+ if (!classMatch) return `<pre class="${className}"${attrs}>`;
21
+ const classes = new Set(classMatch[2].split(/\s+/).filter(Boolean));
22
+ classes.add(className);
23
+ return match.replace(classMatch[0], ` class=${classMatch[1]}${[...classes].join(" ")}${classMatch[1]}`);
24
+ });
14
25
  }