Skip to content

Commit 05d5bd8

Browse files
committed
Attempt to make the spec look stylish
1 parent 98eb591 commit 05d5bd8

File tree

6 files changed

+418
-68
lines changed

6 files changed

+418
-68
lines changed

package-lock.json

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

remark/remark-rfc2119.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { text } from "mdast-builder";
2+
import { visit } from "unist-util-visit";
3+
4+
5+
/**
6+
* RFC 2119 / RFC 8174 keywords
7+
* Order matters: longer phrases first
8+
*/
9+
const KEYWORDS = [
10+
"MUST NOT",
11+
"NOT REQUIRED",
12+
"SHALL NOT",
13+
"SHOULD NOT",
14+
"NOT RECOMMENDED",
15+
"MUST",
16+
"REQUIRED",
17+
"SHALL",
18+
"SHOULD",
19+
"RECOMMENDED",
20+
"MAY",
21+
"OPTIONAL"
22+
];
23+
24+
const KEYWORD_REGEX = new RegExp(`(?:${KEYWORDS.map((k) => k.replace(" ", "\\s")).join("|")})`, "gm");
25+
26+
const skipNodes = new Set(["code", "inlineCode", "link", "definition", "html"]);
27+
28+
export default function remarkRfc2119() {
29+
return (tree) => {
30+
visit(tree, "text", (node, index, parent) => {
31+
// Do not touch code, inlineCode, links, or HTML
32+
if (skipNodes.has(parent.type)) {
33+
return;
34+
}
35+
36+
const value = node.value;
37+
let match;
38+
let lastIndex = 0;
39+
const children = [];
40+
41+
KEYWORD_REGEX.lastIndex = 0;
42+
43+
while ((match = KEYWORD_REGEX.exec(value)) !== null) {
44+
const [keyword] = match;
45+
const start = match.index;
46+
const end = start + keyword.length;
47+
48+
if (start > lastIndex) {
49+
children.push(text(value.slice(lastIndex, start)));
50+
}
51+
52+
const keywordNode = text(keyword);
53+
keywordNode.data = {
54+
hName: "span",
55+
hProperties: {
56+
className: ["rfc2119", keyword.toLowerCase().replace(/\s+/g, "-")]
57+
}
58+
};
59+
children.push(keywordNode);
60+
61+
lastIndex = end;
62+
}
63+
64+
if (children.length === 0) {
65+
return;
66+
}
67+
68+
if (lastIndex < value.length) {
69+
children.push(text(value.slice(lastIndex)));
70+
}
71+
72+
parent.children.splice(index, 1, ...children);
73+
});
74+
};
75+
}

remark/remark-table-of-contents.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ const remarkTableOfContents = (options) => (tree, file) => {
2525
const headingText = nodeToString(headingNode);
2626

2727
if (headingText === options.heading) {
28+
headingNode.data = {
29+
hProperties: {
30+
className: ["toc-heading"]
31+
}
32+
};
33+
tableOfContents.data = {
34+
hProperties: {
35+
className: ["toc"]
36+
}
37+
};
38+
2839
insertTableOfContents = () => {
2940
parent.children.splice(index + 1, 0, tableOfContents);
3041
};
@@ -37,7 +48,7 @@ const remarkTableOfContents = (options) => (tree, file) => {
3748
while (headingNode.depth > currentDepth) {
3849
const newList = list("unordered");
3950
listStack.push(newList);
40-
currentList.children.push(newList);
51+
currentList.children.push(listItem(newList));
4152
currentList = newList;
4253
currentDepth++;
4354
}

specs/.remarkrc-build.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ export default {
1818
[rehypeHighlightCodeLines, { showLineNumbers: true }],
1919
rehypeLinkTransformer,
2020
[rehypeDocument, {
21-
css: [
22-
"https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css",
23-
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/styles/github-dark-dimmed.min.css"
21+
link: [
22+
{
23+
rel: "stylesheet",
24+
href: "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/styles/github-dark-dimmed.min.css",
25+
media: "(prefers-color-scheme: dark)"
26+
},
27+
{
28+
rel: "stylesheet",
29+
href: "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/styles/github.css",
30+
media: "(prefers-color-scheme: light)"
31+
}
2432
],
2533
style: readFileSync(resolve(import.meta.dirname, "spec.css"), "utf8")
2634
}],

specs/.remarkrc-specs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import remarkCodeTitles from "../remark/remark-code-titles.js";
55
import remarkHeadings from "../remark/remark-headings.js";
66
import remarkReferenceLinks from "../remark/remark-reference-links.js";
77
import remarkTableOfContents from "../remark/remark-table-of-contents.js";
8+
import remarkRfc2119 from "../remark/remark-rfc2119.js";
89

910

1011
export default {
@@ -37,6 +38,7 @@ export default {
3738
]
3839
}],
3940
remarkFlexibleContainers,
40-
remarkCodeTitles
41+
remarkCodeTitles,
42+
remarkRfc2119
4143
]
4244
};

0 commit comments

Comments
 (0)