From 24c86ea20e3260244cf7d64125d4c0d757de6eb7 Mon Sep 17 00:00:00 2001 From: Christian Svensson Date: Fri, 1 Aug 2025 12:46:12 +0200 Subject: [PATCH] fix: correct anchors by using heading text only (#3167) Addresses the issue mentioned in https://github.com/denoland/fresh/pull/3163#discussion_r2246708003. The issue was that the `raw` markdown was being used for e.g. `## My title`, thus the slug became `-my-title` instead of `my-title`. Slugs were working correctly for `codespan` in headers, e.g. ``## `someCode` ``. I decided to hard error if unexpected heading syntax is used over sluggifying unexpected content (can become super weird otherwise, e.g. `## `). I can revert to fallbacking to `raw` if preferred, but perhaps with at least trimming leading `-` just in case. --- docs/canary/concepts/middleware.md | 1 + docs/latest/concepts/deployment.md | 4 ++-- docs/toc.ts | 2 +- www/routes/_error.tsx | 2 +- www/utils/markdown.ts | 38 ++++++++++++++++++++---------- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/docs/canary/concepts/middleware.md b/docs/canary/concepts/middleware.md index 472bae42..ac69235a 100644 --- a/docs/canary/concepts/middleware.md +++ b/docs/canary/concepts/middleware.md @@ -52,6 +52,7 @@ const middleware = define.middleware(async (ctx) => { Fresh ships with the following middlewares built-in: - [cors()](/docs/canary/plugins/cors) - Set CORS HTTP headers +- [csrf()](/docs/canary/plugins/cors) - Set CSRF HTTP headers - [trailingSlash()](/docs/canary/plugins/trailing-slashes) - Enforce trailing slashes diff --git a/docs/latest/concepts/deployment.md b/docs/latest/concepts/deployment.md index 2f8a7e17..e33bf70e 100644 --- a/docs/latest/concepts/deployment.md +++ b/docs/latest/concepts/deployment.md @@ -8,8 +8,8 @@ deployed to any system or platform that can run a Deno based web server. Here are instructions for specific providers / systems: -- [Deno Deploy](#-deno-deploy) -- [Docker](#-docker) +- [Deno Deploy](#deno-deploy) +- [Docker](#docker) ## Deno Deploy diff --git a/docs/toc.ts b/docs/toc.ts index fd5b0f58..be3a4bdd 100644 --- a/docs/toc.ts +++ b/docs/toc.ts @@ -85,8 +85,8 @@ const toc: RawTableOfContents = { ["daisyui", "daisyUI", "link:canary"], ["modifying-the-head", "Modifying the ", "link:latest"], ["creating-a-crud-api", "Creating a CRUD API", "link:latest"], + ["markdown", "Rendering Markdown", "link:canary"], ["rendering-raw-html", "Rendering raw HTML", "link:canary"], - ["markdown", "Markdown", "link:canary"], [ "sharing-state-between-islands", "Sharing state between islands", diff --git a/www/routes/_error.tsx b/www/routes/_error.tsx index bef3af28..084e9b02 100644 --- a/www/routes/_error.tsx +++ b/www/routes/_error.tsx @@ -28,7 +28,7 @@ export function ServerCodePage( ); } -export default function PageNotFound(props: PageProps) { +export default function ErrorPage(props: PageProps) { const error = props.error; if (error instanceof HttpError) { if (error.status === 404) { diff --git a/www/utils/markdown.ts b/www/utils/markdown.ts index d230fd69..1dad521c 100644 --- a/www/utils/markdown.ts +++ b/www/utils/markdown.ts @@ -3,6 +3,7 @@ import denoJson from "../../deno.json" with { type: "json" }; export { extractYaml as frontMatter } from "@std/front-matter"; import * as Marked from "marked"; +import { HttpError } from "fresh"; import { asset } from "fresh/runtime"; import { escape as escapeHtml } from "@std/html"; import { mangle } from "marked-mangle"; @@ -108,15 +109,18 @@ class DefaultRenderer extends Marked.Renderer { .replaceAll(/['](.*?)[']/g, "‘$1’"); } - override heading({ - tokens, - depth, - raw, - }: Marked.Tokens.Heading): string { - let slugInput = tokens.length === 1 && tokens[0].type === "codespan" - ? tokens[0].text - : raw; + override heading({ tokens, depth }: Marked.Tokens.Heading): string { + this.#assert(tokens.length > 0, "Markdown heading tokens unexpected value"); + const content = tokens[0]; + this.#assert( + content.type === "text" || content.type === "codespan", + "Markdown heading tokens unexpected value", + ); + + let slugInput = content.text; + + // Rewrites e.g. `.get()` to `get` if (/^\..*\(\)$/.test(slugInput)) { slugInput = slugInput.slice(1, -2); } @@ -216,6 +220,10 @@ class DefaultRenderer extends Marked.Renderer { } return `
\n${this.parser.parse(tokens)}
\n`; } + + #assert(expr: unknown, msg: string): asserts expr { + if (!expr) throw new Error(msg); + } } export interface MarkdownOptions { @@ -231,9 +239,15 @@ export function renderMarkdown( renderer, }; - const html = opts.inline - ? Marked.parseInline(input, markedOpts) as string - : Marked.parse(input, markedOpts) as string; + try { + const html = opts.inline + ? Marked.parseInline(input, markedOpts) as string + : Marked.parse(input, markedOpts) as string; - return { headings: renderer.headings, html }; + return { headings: renderer.headings, html }; + } catch (err) { + throw new HttpError(500, "Markdown parsing error", { + cause: err, + }); + } }