Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/site-kit/src/lib/markdown/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createHash } from 'node:crypto';
import { mkdir, readFile, readdir, stat, writeFile } from 'node:fs/promises';
import path from 'node:path';
import ts from 'typescript';
import { SHIKI_LANGUAGE_MAP, escape, normalizeSlugify, transform } from './utils';
import { SHIKI_LANGUAGE_MAP, escape, normalizeSlugify, smart_quotes, transform } from './utils';
import type { Declaration, TypeElement, Modules } from './index';

type MetadataKeys = 'file' | 'link' | 'copy';
Expand Down Expand Up @@ -235,6 +235,7 @@ async function parse({

/** @type {string} */
const content = await transform(body, {
text: smart_quotes,
heading(html, level, raw) {
const title = html
.replace(/<\/?code>/g, '')
Expand Down
14 changes: 14 additions & 0 deletions packages/site-kit/src/lib/markdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ export const normalizeSlugify = (str: string) => {
return slugify(removeHTMLEntities(removeMarkdown(str))).replace(/(<([^>]+)>)/gi, '');
};

export function smart_quotes(str: string) {
// replace dumb quotes with smart quotes. This isn't a perfect algorithm — it
// wouldn't correctly handle `That '70s show` or `My country 'tis of thee`
// but a) it's very unlikely they'll occur in our docs, and
// b) they can be dealt with manually
return str.replace(/(.|^)(&#39;|&quot;)(.|$)/g, (m, before, quote, after) => {
const left = !before.trim();
const double = quote === '&quot;';
const entity = `&${left ? 'l' : 'r'}${double ? 'd' : 's'}quo;`;

return (before ?? '') + entity + (after ?? '');
});
}

const default_renderer: Partial<Renderer> = {
code(code, infostring, escaped) {
const lang = infostring?.match(/\S*/)?.[0];
Expand Down
5 changes: 4 additions & 1 deletion packages/site-kit/src/lib/server/content/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extract_frontmatter, slugify } from '../../markdown/utils';
import { extract_frontmatter, slugify, smart_quotes } from '../../markdown/utils';
import type { Document } from '../../types';

export async function create_index(
Expand All @@ -24,6 +24,9 @@ export async function create_index(
throw new Error(`Missing title in ${slug} frontmatter`);
}

metadata.title = smart_quotes(metadata.title);
if (metadata.description) metadata.description = smart_quotes(metadata.description);

const sections = Array.from(body.matchAll(/^##\s+(.*)$/gm)).map((match) => {
const title = match[1].replace(/`/g, '').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
const slug = slugify(title);
Expand Down
Loading