-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.js
157 lines (132 loc) · 3.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { base as app_base } from '$app/paths';
import {
escape,
extractFrontmatter,
markedTransform,
normalizeSlugify,
removeMarkdown
} from '@sveltejs/site-kit/markdown';
import { CONTENT_BASE_PATHS } from '../../../constants.js';
import { render_content } from '../renderer';
/**
* @param {import('./types').DocsData} docs_data
* @param {string} slug
*/
export async function get_parsed_docs(docs_data, slug) {
for (const { pages } of docs_data) {
for (const page of pages) {
if (page.slug === slug) {
return {
...page,
content: await render_content(page.file, page.content)
};
}
}
}
return null;
}
/** @return {Promise<import('./types').DocsData>} */
export async function get_docs_data(base = CONTENT_BASE_PATHS.DOCS) {
const { readdir, readFile } = await import('node:fs/promises');
/** @type {import('./types').DocsData} */
const docs_data = [];
for (const category_dir of await readdir(base)) {
const match = /\d{2}-(.+)/.exec(category_dir);
if (!match) continue;
const category_slug = match[1];
// Read the index.md
const { title: category_title, draft = 'false' } = extractFrontmatter(
await readFile(`${base}/${category_dir}/index.md`, 'utf-8')
).metadata;
if (draft === 'true') continue;
/** @type {import('./types').Category} */
const category = {
title: category_title,
slug: category_slug,
pages: []
};
for (const filename of await readdir(`${base}/${category_dir}`)) {
if (filename === 'index.md') continue;
const match = /\d{2}-(.+)/.exec(filename);
if (!match) continue;
const page_slug = match[1].replace('.md', '');
const page_data = extractFrontmatter(
await readFile(`${base}/${category_dir}/${filename}`, 'utf-8')
);
if (page_data.metadata.draft === 'true') continue;
const page_title = page_data.metadata.title;
const page_content = page_data.body;
category.pages.push({
title: page_title,
slug: page_slug,
content: page_content,
category: category_title,
sections: await get_sections(page_content),
path: `${app_base}/docs/${page_slug}`,
file: `${category_dir}/${filename}`
});
}
docs_data.push(category);
}
return docs_data;
}
/** @param {import('./types').DocsData} docs_data */
export function get_docs_list(docs_data) {
return docs_data.map((category) => ({
title: category.title,
pages: category.pages.map((page) => ({
title: page.title,
path: page.path
}))
}));
}
/** @param {string} str */
const titled = async (str) =>
removeMarkdown(
escape(await markedTransform(str, { paragraph: (txt) => txt }))
.replace(/<\/?code>/g, '')
.replace(/'/g, "'")
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/, '&')
.replace(/<(\/)?(em|b|strong|code)>/g, '')
);
/**
* @param {string} markdown
* @returns {Promise<import('./types').Section[]>}
*/
export async function get_sections(markdown) {
const lines = markdown.split('\n');
const root = /** @type {import('./types').Section} */ ({
title: 'Root',
slug: 'root',
sections: [],
breadcrumbs: ['']
});
let currentNodes = [root];
for (const line of lines) {
const match = line.match(/^(#{2,4})\s(.*)/);
if (match) {
const level = match[1].length - 2;
const text = await titled(match[2]);
const slug = normalizeSlugify(text);
// Prepare new node
/** @type {import('./types').Section} */
const newNode = {
title: text,
slug,
sections: [],
breadcrumbs: [...currentNodes[level].breadcrumbs, text]
};
// Add the new node to the tree
const sections = currentNodes[level].sections;
if (!sections) throw new Error(`Could not find section ${level}`);
sections.push(newNode);
// Prepare for potential children of the new node
currentNodes = currentNodes.slice(0, level + 1);
currentNodes.push(newNode);
}
}
return /** @type {import('./types').Section[]} */ (root.sections);
}