pull/8435/head
Puru Vijay 3 years ago
parent 9ed6d09bf8
commit 302bae7839

@ -1,7 +1,13 @@
// @ts-check // @ts-check
import { base as app_base } from '$app/paths'; import { base as app_base } from '$app/paths';
import fs from 'fs'; import fs from 'fs';
import { extract_frontmatter, normalizeSlugify, removeMarkdown, transform } from '../markdown'; import {
escape,
extract_frontmatter,
normalizeSlugify,
removeMarkdown,
transform
} from '../markdown';
const BASE = '../../site/content/docs/'; const BASE = '../../site/content/docs/';
@ -79,11 +85,12 @@ function get_sections(markdown) {
while ((match = headingRegex.exec(markdown)) !== null) { while ((match = headingRegex.exec(markdown)) !== null) {
secondLevelHeadings.push({ secondLevelHeadings.push({
title: removeMarkdown( title: removeMarkdown(
transform(match[1], { paragraph: (txt) => txt }) escape(transform(match[1], { paragraph: (txt) => txt }))
.replace(/<\/?code>/g, '') .replace(/<\/?code>/g, '')
.replace(/&quot;/g, '"') .replace(/&quot;/g, '"')
.replace(/&lt;/g, '<') .replace(/&lt;/g, '<')
.replace(/&gt;/g, '>') .replace(/&gt;/g, '>')
.replace(/<(\/)?(em|b|strong|code)>/g, '')
), ),
slug: normalizeSlugify(match[1]) slug: normalizeSlugify(match[1])
}); });

@ -51,12 +51,13 @@ export function slugify(title) {
/** @param {string} markdown */ /** @param {string} markdown */
export function removeMarkdown(markdown) { export function removeMarkdown(markdown) {
return markdown return markdown
.replace(/\*\*(.+?)\*\*/g, '$1') .replace(/\*\*(.+?)\*\*/g, '$1') // bold
.replace(/\*(.+?)\*/g, '$1') .replace(/_(.+?)_/g, '$1') // Italics
.replace(/`(.+?)`/g, '$1') .replace(/\*(.+?)\*/g, '$1') // Italics
.replace(/~~(.+?)~~/g, '$1') .replace(/`(.+?)`/g, '$1') // Inline code
.replace(/\[(.+?)\]\(.+?\)/g, '$1') .replace(/~~(.+?)~~/g, '$1') // Strikethrough
.replace(/\n/g, ' ') .replace(/\[(.+?)\]\(.+?\)/g, '$1') // Link
.replace(/\n/g, ' ') // New line
.replace(/ {2,}/g, ' ') .replace(/ {2,}/g, ' ')
.trim(); .trim();
} }

@ -46,7 +46,7 @@ export function content() {
const { body, metadata } = extract_frontmatter(markdown); const { body, metadata } = extract_frontmatter(markdown);
const sections = body.trim().split(/^### /m); const sections = body.trim().split(/^## /m);
const intro = sections.shift().trim(); const intro = sections.shift().trim();
const rank = +metadata.rank || undefined; const rank = +metadata.rank || undefined;
@ -59,32 +59,32 @@ export function content() {
for (const section of sections) { for (const section of sections) {
const lines = section.split('\n'); const lines = section.split('\n');
const h3 = lines.shift(); const h2 = lines.shift();
const content = lines.join('\n'); const content = lines.join('\n');
const subsections = content.trim().split('### '); const subsections = content.trim().split('## ');
const intro = subsections.shift().trim(); const intro = subsections.shift().trim();
blocks.push({ blocks.push({
breadcrumbs: [...breadcrumbs, removeMarkdown(metadata.title), removeMarkdown(h3)], breadcrumbs: [...breadcrumbs, removeMarkdown(metadata.title), removeMarkdown(h2)],
href: category.href([slug, normalizeSlugify(h3)]), href: category.href([slug, normalizeSlugify(h2)]),
content: plaintext(intro), content: plaintext(intro),
rank rank
}); });
for (const subsection of subsections) { for (const subsection of subsections) {
const lines = subsection.split('\n'); const lines = subsection.split('\n');
const h4 = lines.shift(); const h3 = lines.shift();
blocks.push({ blocks.push({
breadcrumbs: [ breadcrumbs: [
...breadcrumbs, ...breadcrumbs,
removeMarkdown(metadata.title), removeMarkdown(metadata.title),
removeMarkdown(h3), removeMarkdown(h2),
removeMarkdown(h4) removeMarkdown(h3)
], ],
href: category.href([slug, normalizeSlugify(h3), normalizeSlugify(h4)]), href: category.href([slug, normalizeSlugify(h2), normalizeSlugify(h3)]),
content: plaintext(lines.join('\n').trim()), content: plaintext(lines.join('\n').trim()),
rank rank
}); });

Loading…
Cancel
Save