Skip to content

Commit 9c9c8f5

Browse files
committed
adjust formatting to match kit docs
1 parent 36a62bb commit 9c9c8f5

File tree

9 files changed

+149
-201
lines changed

9 files changed

+149
-201
lines changed

dist/main.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5480,7 +5480,7 @@ var counter_1 = counter;
54805480
slugify_1.counter = counter_1;
54815481

54825482
const SLUG_PRESERVE_UNICODE$1 = false;
5483-
const SLUG_SEPARATOR$1 = "_";
5483+
const SLUG_SEPARATOR$1 = "-";
54845484

54855485

54865486

@@ -5510,10 +5510,10 @@ function url_safe_processor(
55105510
],
55115511
separator,
55125512
decamelize: false,
5513-
lowercase: false,
5513+
lowercase: true,
55145514
})
5515-
.replace(/DOLLAR/g, "$")
5516-
.replace(/DASH/g, "-");
5515+
.replace(/DOLLAR/gi, "$")
5516+
.replace(/DASH/gi, "-");
55175517
}
55185518

55195519
const alpha_num_regex = /[a-zA-Z0-9]/;
@@ -5560,7 +5560,8 @@ function unicode_safe_processor(
55605560

55615561
return accum;
55625562
}, [] )
5563-
.join(separator);
5563+
.join(separator)
5564+
.toLowerCase();
55645565
}
55655566

55665567
function make_session_slug_processor({
@@ -7752,7 +7753,7 @@ function highlight(source, lang) {
77527753
}
77537754

77547755
const SLUG_PRESERVE_UNICODE = false;
7755-
const SLUG_SEPARATOR = "_";
7756+
const SLUG_SEPARATOR = "-";
77567757

77577758
const make_slug = make_session_slug_processor({
77587759
preserve_unicode: SLUG_PRESERVE_UNICODE,
@@ -7795,6 +7796,15 @@ function hr_renderer() {
77957796
let prev_level = 3;
77967797
let sections = [];
77977798
let section_stack = [sections];
7799+
let dir = "";
7800+
let section_title = "";
7801+
7802+
function get_slug_segments() {
7803+
return section_stack.map((section, i) => {
7804+
if (i > 0) return "";
7805+
return section[section.length - 1].slug;
7806+
});
7807+
}
77987808

77997809
function heading_renderer(
78007810
text,
@@ -7803,14 +7813,11 @@ function heading_renderer(
78037813
) {
78047814
let slug;
78057815

7806-
const match = /<a href="([^"]+)"[^>]*>(.+)<\/a>/.exec(text);
7807-
7808-
if (match) {
7809-
slug = match[1];
7810-
text = match[2];
7811-
} else {
7812-
slug = make_slug(rawtext);
7813-
}
7816+
slug = make_slug(
7817+
level === 3
7818+
? [section_title, rawtext].join(" ")
7819+
: [...get_slug_segments(), rawtext].join(" ")
7820+
);
78147821

78157822
if (level === 3 || level === 4) {
78167823
const title = text.replace(/<\/?code>/g, "");
@@ -7835,7 +7842,7 @@ function heading_renderer(
78357842
<span id="${slug}" class="offset-anchor" ${
78367843
level > 4 ? "data-scrollignore" : ""
78377844
}></span>
7838-
<a href="docs#${slug}" class="anchor" aria-hidden="true"></a>
7845+
<a href="${dir}#${slug}" class="anchor" aria-hidden="true"></a>
78397846
${text}
78407847
</h${level}>`;
78417848
}
@@ -7850,6 +7857,7 @@ renderer.hr = hr_renderer;
78507857
function format_api(
78517858
file,
78527859
markdown,
7860+
directory,
78537861
name
78547862
) {
78557863
const {
@@ -7862,10 +7870,12 @@ function format_api(
78627870
const section_slug = make_slug(title);
78637871

78647872
// reset the stateful stuff
7873+
dir = directory;
78657874
prev_level = 3;
78667875
sections = [];
78677876
section_stack = [sections];
78687877
block_open = false;
7878+
section_title = title;
78697879

78707880
const html = marked_1(content, { renderer });
78717881

@@ -7998,13 +8008,13 @@ async function run() {
79988008

79998009
= pkg_docs.map(([name, content]) => [
80008010
name,
8001-
format_api(name, increment_headings(content), name),
8011+
format_api(name, increment_headings(content), "", name),
80028012
]);
80038013

80048014
console.log(formatted_pkg_docs, null, 2);
80058015

80068016
const formatted_base_docs = base_docs.api.map(([name, content]) =>
8007-
format_api(name, content)
8017+
format_api(name, content, "docs")
80088018
);
80098019
console.log(JSON.stringify(formatted_base_docs, null, 2));
80108020

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"test": "uvu -r ts-node/register src test.ts$",
88
"build": "rollup -c",
9-
"ts": "tsc --noEmit"
9+
"ts": "tsc --noEmit",
10+
"generate_fixtures": "node -r ts-node/register src/format/fixtures/generateFixtures.ts"
1011
},
1112
"repository": {
1213
"type": "git",

src/format/fixtures/api-docs-html.js

Lines changed: 1 addition & 97 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { promises as fs } from "fs";
2+
import * as path from "path";
3+
import { format_api } from "../format_api";
4+
import markdown from "./api-docs-markdown";
5+
6+
const output_path = path.join(
7+
process.cwd(),
8+
"src",
9+
"format",
10+
"fixtures",
11+
"api-docs-html.js"
12+
);
13+
// const input = path.join(dir, "api-docs-markdown.js");
14+
15+
async function run() {
16+
const formatted = format_api("./api-docs.md", markdown, "docs");
17+
await fs.writeFile(
18+
output_path,
19+
`export default ${JSON.stringify(formatted)}`
20+
);
21+
}
22+
23+
run();

src/format/format_api.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import api_output from "./fixtures/api-docs-html.js";
99
const format = suite("transform docs");
1010

1111
format("formats the api docs", () => {
12-
const output = format_api("./api-docs.md", api);
12+
const output = format_api("./api-docs.md", api, "docs");
1313

1414
assert.equal(output, api_output);
1515
});
@@ -20,8 +20,8 @@ format("formats the api docs", () => {
2020

2121
format.skip("duplicate slugs should throw an error", () => {
2222
assert.throws(() => {
23-
format_api("./api-docs.md", api);
24-
format_api("./api-docs.md", api);
23+
format_api("./api-docs.md", api, "docs");
24+
format_api("./api-docs.md", api, "docs");
2525
}, /Duplicate slug Template_syntax/);
2626
});
2727

src/format/format_api.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { highlight } from "./highlight";
88
import type { language } from "./highlight";
99

1010
export const SLUG_PRESERVE_UNICODE = false;
11-
export const SLUG_SEPARATOR = "_";
11+
export const SLUG_SEPARATOR = "-";
1212

1313
const make_slug = make_session_slug_processor({
1414
preserve_unicode: SLUG_PRESERVE_UNICODE,
@@ -51,6 +51,15 @@ function hr_renderer(): string {
5151
let prev_level = 3;
5252
let sections: section[] = [];
5353
let section_stack = [sections];
54+
let dir = "";
55+
let section_title = "";
56+
57+
function get_slug_segments(): string[] {
58+
return section_stack.map((section, i) => {
59+
if (i > 0) return "";
60+
return section[section.length - 1].slug;
61+
});
62+
}
5463

5564
function heading_renderer(
5665
text: string,
@@ -59,14 +68,11 @@ function heading_renderer(
5968
): string {
6069
let slug;
6170

62-
const match = /<a href="([^"]+)"[^>]*>(.+)<\/a>/.exec(text);
63-
64-
if (match) {
65-
slug = match[1];
66-
text = match[2];
67-
} else {
68-
slug = make_slug(rawtext);
69-
}
71+
slug = make_slug(
72+
level === 3
73+
? [section_title, rawtext].join(" ")
74+
: [...get_slug_segments(), rawtext].join(" ")
75+
);
7076

7177
if (level === 3 || level === 4) {
7278
const title = text.replace(/<\/?code>/g, "");
@@ -91,7 +97,7 @@ function heading_renderer(
9197
<span id="${slug}" class="offset-anchor" ${
9298
level > 4 ? "data-scrollignore" : ""
9399
}></span>
94-
<a href="docs#${slug}" class="anchor" aria-hidden="true"></a>
100+
<a href="${dir}#${slug}" class="anchor" aria-hidden="true"></a>
95101
${text}
96102
</h${level}>`;
97103
}
@@ -106,6 +112,7 @@ renderer.hr = hr_renderer;
106112
export function format_api(
107113
file: string,
108114
markdown: string,
115+
directory: string,
109116
name?: string
110117
): FormattedFile {
111118
const {
@@ -118,10 +125,12 @@ export function format_api(
118125
const section_slug = make_slug(title);
119126

120127
// reset the stateful stuff
128+
dir = directory;
121129
prev_level = 3;
122130
sections = [];
123131
section_stack = [sections];
124132
block_open = false;
133+
section_title = title;
125134

126135
const html = marked(content, { renderer });
127136

0 commit comments

Comments
 (0)