Files
sweet/www/utils/markdown.ts
T
Christian Svensson a2de5154ec fix(docs): improve and increase consistency for code blocks (#3163)
Follow-up from #3162.

This PR does various tweaks, mostly related to code blocks.

- Adds missing titles to code blocks, and updates titles to be more
consistent
- Adds filenames to most ` ```tsx` blocks with e.g. `main.ts`, `dev.ts`
or a suitable example (e.g. `routes/greeting.tsx`) based on the code
- Adds filenames to ` ```diff` blocks if there's a relevant file, to
make the icon e.g. TS over Diff icon
  - Code blocks with Fresh code (e.g. interfaces) uses ` ```tsx fresh 🍋`
- Changes most ` ```sh` blocks with example terminal commands to ` ```sh
Terminal`
- Updates ` ```txt Project Structure` or ` ```diff Project Structure`
blocks to be more consistent
- Changes `sh` or `txt` to `txt-files` which improves highlighting and
makes icon consistent
- Renames `<project dir>` to `<project root>` to match builder `root`
option terminology
- Adds `<project root>` root folder to most/all overview folder
overviews
  - Makes the structure blocks more consistently displayed
- Tried to make some "bare" examples more complete

## Comparison

Here's a small curated set of before/after changes.

### Deployment

- `deno` highlighted in terminal commands
- `.gitignore` syntax highlighting works without using `sh` as language

<img width="1641" height="1009" alt="image"
src="https://github.com/user-attachments/assets/170cf833-1f6a-4b0d-8d2d-a081bc28a967"
/>

### File routing

Updated icon, comments same as before, but root and hierarchy have new
colors (`operator`/`function`).

<img width="1647" height="588" alt="image"
src="https://github.com/user-attachments/assets/f28db51b-4bee-45e4-be1c-e137a6d3ae3d"
/>
2025-08-01 09:07:42 +02:00

240 lines
6.3 KiB
TypeScript

import denoJson from "../../deno.json" with { type: "json" };
export { extractYaml as frontMatter } from "@std/front-matter";
import * as Marked from "marked";
import { asset } from "fresh/runtime";
import { escape as escapeHtml } from "@std/html";
import { mangle } from "marked-mangle";
import GitHubSlugger from "github-slugger";
import { Prism } from "./prism.ts";
const slugger = new GitHubSlugger();
Marked.marked.use(mangle());
const ADMISSION_REG = /^\[(info|warn|tip)\]:\s/;
const LOGOS = [
{
lang: /^tsx?$/,
file: /\.tsx?$/,
src: asset("/logos/typescript.svg"),
text: "Typescript",
},
{
lang: /^css$/,
file: /\.css$/,
src: asset("/logos/css.svg"),
text: "CSS",
},
{
lang: /^html$/,
file: /\.html$/,
src: asset("/logos/html.svg"),
text: "HTML",
},
{
lang: /^jsonc?$/,
file: /\.jsonc?$/,
src: asset("/logos/json.svg"),
text: "JSON",
},
{
lang: /^(sh|bash)$/,
file: /\.sh$/,
src: asset("/logos/shell.svg"),
text: "Terminal (Shell/Bash)",
},
{
lang: /^md$/,
file: /\.md$/,
src: asset("/logos/markdown.svg"),
text: "Markdown",
},
{
lang: /^txt(-files)?$/,
file: /\.txt$/,
src: asset("/logos/text.svg"),
text: "Text",
},
{
lang: /^diff$/,
file: /\.diff$/,
src: asset("/logos/diff.svg"),
text: "File diff",
},
{
lang: /^gitignore$/,
file: /^\.gitignore$/,
src: asset("/logos/git.svg"),
text: "Git",
},
{
lang: /^dockerfile$/,
file: /^Dockerfile$/,
src: asset("/logos/docker.svg"),
text: "Docker",
},
];
export interface MarkdownHeading {
id: string;
html: string;
}
class DefaultRenderer extends Marked.Renderer {
headings: MarkdownHeading[] = [];
override text(
token: Marked.Tokens.Text | Marked.Tokens.Escape | Marked.Tokens.Tag,
): string {
if (
token.type === "text" && "tokens" in token && token.tokens !== undefined
) {
return this.parser.parseInline(token.tokens);
}
// Smartypants typography enhancement
return token.text
.replaceAll("...", "&#8230;")
.replaceAll("--", "&#8212;")
.replaceAll("---", "&#8211;")
.replaceAll(/(\w)'(\w)/g, "$1&#8217;$2")
.replaceAll(/s'/g, "s&#8217;")
.replaceAll("&#39;", "&#8217;")
.replaceAll(/["](.*?)["]/g, "&#8220;$1&#8221")
.replaceAll(/&quot;(.*?)&quot;/g, "&#8220;$1&#8221")
.replaceAll(/['](.*?)[']/g, "&#8216;$1&#8217;");
}
override heading({
tokens,
depth,
raw,
}: Marked.Tokens.Heading): string {
let slugInput = tokens.length === 1 && tokens[0].type === "codespan"
? tokens[0].text
: raw;
if (/^\..*\(\)$/.test(slugInput)) {
slugInput = slugInput.slice(1, -2);
}
const slug = slugger.slug(slugInput);
const text = this.parser.parseInline(tokens);
this.headings.push({ id: slug, html: text });
return `<h${depth} id="${slug}"><a class="md-anchor" tabindex="-1" href="#${slug}">${text}<span aria-hidden="true">#</span></a></h${depth}>`;
}
override link({ href, title, tokens }: Marked.Tokens.Link) {
const text = this.parser.parseInline(tokens);
const titleAttr = title ? ` title="${title}"` : "";
if (href.startsWith("#")) {
return `<a href="${href}"${titleAttr}>${text}</a>`;
}
return `<a href="${href}"${titleAttr} rel="noopener noreferrer">${text}</a>`;
}
override image({ href, text, title }: Marked.Tokens.Image) {
return `<img src="${href}" alt="${text ?? ""}" title="${title ?? ""}" />`;
}
override code({ lang: info, text }: Marked.Tokens.Code): string {
// For canary only
text = text.replaceAll(
/(@fresh\/init)/g,
`@fresh/init@${denoJson.version}`,
);
// format: tsx
// format: tsx my/file.ts
// format: tsx "This is my title"
let lang = "";
let title = "";
const match = info?.match(/^([\w_-]+)\s*(.*)?$/);
if (match) {
lang = match[1].toLocaleLowerCase();
title = match[2] ?? "";
}
// Find icon by filename first, then by markdown block language.
const icon = LOGOS.find((l) => l.file.test(title)) ??
LOGOS.find((l) => l.lang.test(lang));
let out = `<div class="fenced-code">`;
if (title || icon) {
const image = icon
? `<img src="${icon.src}" alt="${icon.text}" title="${icon.text}">`
: "";
out += `<div class="fenced-code-header">
<span class="fenced-code-title lang-${lang}">
${image}
${title ? escapeHtml(String(title)) : "&nbsp;"}
</span>
</div>`;
}
const grammar = lang && Object.hasOwnProperty.call(Prism.languages, lang)
? Prism.languages[lang]
: undefined;
if (grammar === undefined) {
out += `<pre><code class="notranslate">${escapeHtml(text)}</code></pre>`;
} else {
const html = Prism.highlight(text, grammar, lang);
out +=
`<pre class="highlight highlight-source-${lang} notranslate lang-${lang}"><code>${html}</code></pre>`;
}
out += `</div>`;
return out;
}
override blockquote({ text, tokens }: Marked.Tokens.Blockquote): string {
const match = text.match(ADMISSION_REG);
if (match) {
const label: Record<string, string> = {
tip: "Tip",
warn: "Warning",
info: "Info",
};
Marked.walkTokens(tokens, (token) => {
if (token.type === "text" && token.text.startsWith(match[0])) {
token.text = token.text.slice(match[0].length);
}
});
const type = match[1];
const icon = `<svg class="icon"><use href="/icons.svg#${type}" /></svg>`;
return `<blockquote class="admonition ${type}">\n<span class="admonition-header">${icon}${
label[type]
}</span>${this.parser.parse(tokens)}</blockquote>\n`;
}
return `<blockquote>\n${this.parser.parse(tokens)}</blockquote>\n`;
}
}
export interface MarkdownOptions {
inline?: boolean;
}
export function renderMarkdown(
input: string,
opts: MarkdownOptions = {},
): { headings: MarkdownHeading[]; html: string } {
const renderer = new DefaultRenderer();
const markedOpts: Marked.MarkedOptions = {
gfm: true,
renderer,
};
const html = opts.inline
? Marked.parseInline(input, markedOpts) as string
: Marked.parse(input, markedOpts) as string;
return { headings: renderer.headings, html };
}