Skip to content

Commit 9af0a18

Browse files
committed
add more docs, a few renames
1 parent e5c2797 commit 9af0a18

File tree

7 files changed

+39
-10
lines changed

7 files changed

+39
-10
lines changed

src/format/highlight.test.ts renamed to src/format/code.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Root, Paragraph } from "mdast";
55
import unified from "unified";
66
import markdown from "remark-parse";
77

8-
import { highlight, highight_code_block } from "./highlight";
8+
import { highlight, highight_code_block } from "./code";
99

1010
const _highlight = suite("highlight");
1111
const highlight_block = suite("highight_code_block");

src/format/highlight.ts renamed to src/format/code.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ enum LanguageMap {
1919
"" = "",
2020
}
2121

22+
/**
23+
* Custom highlighting for code blocks using PrismJS.
24+
*/
2225
export function highlight(source: string, lang?: language): string {
2326
const plang = lang === "" ? "" : LanguageMap[lang];
2427
const highlighted = plang
@@ -31,12 +34,18 @@ export function highlight(source: string, lang?: language): string {
3134
return `<div class="code-block"><pre class='language-${plang}'><code>${highlighted}</code></pre></div>`;
3235
}
3336

37+
/**
38+
* Custom highlighting for code blocks using PrismJS.
39+
*/
3440
export function highight_code_block(): Transformer {
3541
return function transformer(tree) {
3642
visit(tree, "code", (node: Code) => {
3743
node.value = highlight(node.value, node.lang as language);
3844
//@ts-ignore
3945
node.data = {};
46+
// data.code_block is set so we can differentiate between code blocks and other raw nodes
47+
// this is utilised in a rehype plugin later in the pipeline (split-view)
48+
// retaining this metadata requires a custom handler for raw nodes when converting MDAST -> HAST
4049
node.data.code_block = true;
4150
((node as unknown) as HTML).type = "html";
4251
});

src/format/format_api.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import stringify from "rehype-stringify";
1717
import vFile from "vfile";
1818

1919
import {
20+
strip_h1,
2021
linkify_headings,
2122
validate_headings,
2223
increment_headings,
2324
} from "./headings";
24-
import { links } from "./links";
25-
import { highight_code_block } from "./highlight";
25+
import { set_link_attributes } from "./links";
26+
import { highight_code_block } from "./code";
2627
import { parse_frontmatter } from "./frontmatter";
2728
import { split_view } from "./split_view";
2829
import { custom_vfile, section } from "./types";
@@ -38,10 +39,11 @@ const { process } = unified()
3839
.use(extract_frontmatter)
3940
// MDAST transforms
4041
.use(parse_frontmatter)
42+
.use(strip_h1)
4143
.use(increment_headings)
4244
.use(validate_headings)
4345
.use(linkify_headings)
44-
.use(links)
46+
.use(set_link_attributes)
4547
.use(highight_code_block)
4648
// MDAST -> HAST
4749
.use(rehype, {

src/format/links.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { Root, Paragraph } from "mdast";
55
import unified from "unified";
66
import markdown from "remark-parse";
77

8-
import { links } from "./links";
8+
import { set_link_attributes } from "./links";
99

10-
const processor = unified().use(markdown).use(links);
10+
const processor = unified().use(markdown).use(set_link_attributes);
1111

1212
const _links = suite("links");
1313

src/format/links.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import type { Link, HTML } from "mdast";
33

44
import visit from "unist-util-visit";
55

6-
export function links(): Transformer {
6+
/**
7+
* All links must have `rel="noopener noreferrer"` and external attributes
8+
* must open in a new window.
9+
*/
10+
export function set_link_attributes(): Transformer {
711
return function transformer(tree) {
812
visit(tree, "link", (node: Link) => {
913
let target_attr = "";

src/format/split_view.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import rehype from "remark-rehype";
77
import stringify from "rehype-stringify";
88
import vFile from "vfile";
99

10-
import { highight_code_block } from "./highlight";
10+
import { highight_code_block } from "./code";
1111
import { split_view } from "./split_view";
1212

1313
import u from "unist-builder";

src/format/split_view.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import type { Parent } from "unist";
33
import { custom_vfile } from "./types";
44
import visit from "unist-util-visit";
55

6+
/**
7+
* The svelte docs have a special two-column display for code examples. This
8+
* allows arbitrary content to appear alongside a highlighted code example.
9+
*
10+
* This formatting starts at `***` (a `hr`) and ends with the next fenced code block. All
11+
* of this content is wrappeed in `<div class="side-by-side" />`. then there
12+
* are two nested divs `<div class="copy" />` which contains the arbirtary
13+
* content *up to* the code block and `<div class="code" />` which contains
14+
* the code block itself.
15+
*/
616
export function split_view(): Transformer {
717
return function (tree, vFile: custom_vfile) {
818
if (vFile.data.docs_type !== "docs") return;
@@ -23,16 +33,20 @@ export function split_view(): Transformer {
2333
children: [],
2434
};
2535

36+
// starting from the current node, we iterate the children of the parent
37+
// when we find the next code block (which is a `raw` node at this point)
38+
// we stop, as we have all of the content
2639
for (let index = i + 1; index < parent.children.length; index++) {
2740
const _node = parent.children[index];
2841

2942
if (!_node) break;
3043

44+
// data.code_block is set as meta data when highlight code blocks in another transform
3145
if (_node.data && _node.data.code_block) {
3246
const parts = parent.children.splice(i + 1, index - i);
33-
parts.pop();
47+
3448
left.children = parts;
35-
right.children.push(_node);
49+
right.children.push(parts.pop());
3650
break;
3751
}
3852
}

0 commit comments

Comments
 (0)