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. `## <button ...>My heading</button>`). I can revert to fallbacking to `raw` if preferred, but perhaps with at least trimming leading `-` just in case.
This commit is contained in:
@@ -52,6 +52,7 @@ const middleware = define.middleware(async (ctx) => {
|
|||||||
Fresh ships with the following middlewares built-in:
|
Fresh ships with the following middlewares built-in:
|
||||||
|
|
||||||
- [cors()](/docs/canary/plugins/cors) - Set CORS HTTP headers
|
- [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
|
- [trailingSlash()](/docs/canary/plugins/trailing-slashes) - Enforce trailing
|
||||||
slashes
|
slashes
|
||||||
|
|
||||||
|
|||||||
@@ -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:
|
Here are instructions for specific providers / systems:
|
||||||
|
|
||||||
- [Deno Deploy](#-deno-deploy)
|
- [Deno Deploy](#deno-deploy)
|
||||||
- [Docker](#-docker)
|
- [Docker](#docker)
|
||||||
|
|
||||||
## Deno Deploy
|
## Deno Deploy
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -85,8 +85,8 @@ const toc: RawTableOfContents = {
|
|||||||
["daisyui", "daisyUI", "link:canary"],
|
["daisyui", "daisyUI", "link:canary"],
|
||||||
["modifying-the-head", "Modifying the <head>", "link:latest"],
|
["modifying-the-head", "Modifying the <head>", "link:latest"],
|
||||||
["creating-a-crud-api", "Creating a CRUD API", "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"],
|
["rendering-raw-html", "Rendering raw HTML", "link:canary"],
|
||||||
["markdown", "Markdown", "link:canary"],
|
|
||||||
[
|
[
|
||||||
"sharing-state-between-islands",
|
"sharing-state-between-islands",
|
||||||
"Sharing state between islands",
|
"Sharing state between islands",
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export function ServerCodePage(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function PageNotFound(props: PageProps) {
|
export default function ErrorPage(props: PageProps) {
|
||||||
const error = props.error;
|
const error = props.error;
|
||||||
if (error instanceof HttpError) {
|
if (error instanceof HttpError) {
|
||||||
if (error.status === 404) {
|
if (error.status === 404) {
|
||||||
|
|||||||
+26
-12
@@ -3,6 +3,7 @@ import denoJson from "../../deno.json" with { type: "json" };
|
|||||||
export { extractYaml as frontMatter } from "@std/front-matter";
|
export { extractYaml as frontMatter } from "@std/front-matter";
|
||||||
|
|
||||||
import * as Marked from "marked";
|
import * as Marked from "marked";
|
||||||
|
import { HttpError } from "fresh";
|
||||||
import { asset } from "fresh/runtime";
|
import { asset } from "fresh/runtime";
|
||||||
import { escape as escapeHtml } from "@std/html";
|
import { escape as escapeHtml } from "@std/html";
|
||||||
import { mangle } from "marked-mangle";
|
import { mangle } from "marked-mangle";
|
||||||
@@ -108,15 +109,18 @@ class DefaultRenderer extends Marked.Renderer {
|
|||||||
.replaceAll(/['](.*?)[']/g, "‘$1’");
|
.replaceAll(/['](.*?)[']/g, "‘$1’");
|
||||||
}
|
}
|
||||||
|
|
||||||
override heading({
|
override heading({ tokens, depth }: Marked.Tokens.Heading): string {
|
||||||
tokens,
|
this.#assert(tokens.length > 0, "Markdown heading tokens unexpected value");
|
||||||
depth,
|
|
||||||
raw,
|
|
||||||
}: Marked.Tokens.Heading): string {
|
|
||||||
let slugInput = tokens.length === 1 && tokens[0].type === "codespan"
|
|
||||||
? tokens[0].text
|
|
||||||
: raw;
|
|
||||||
|
|
||||||
|
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)) {
|
if (/^\..*\(\)$/.test(slugInput)) {
|
||||||
slugInput = slugInput.slice(1, -2);
|
slugInput = slugInput.slice(1, -2);
|
||||||
}
|
}
|
||||||
@@ -216,6 +220,10 @@ class DefaultRenderer extends Marked.Renderer {
|
|||||||
}
|
}
|
||||||
return `<blockquote>\n${this.parser.parse(tokens)}</blockquote>\n`;
|
return `<blockquote>\n${this.parser.parse(tokens)}</blockquote>\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#assert(expr: unknown, msg: string): asserts expr {
|
||||||
|
if (!expr) throw new Error(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MarkdownOptions {
|
export interface MarkdownOptions {
|
||||||
@@ -231,9 +239,15 @@ export function renderMarkdown(
|
|||||||
renderer,
|
renderer,
|
||||||
};
|
};
|
||||||
|
|
||||||
const html = opts.inline
|
try {
|
||||||
? Marked.parseInline(input, markedOpts) as string
|
const html = opts.inline
|
||||||
: Marked.parse(input, markedOpts) as string;
|
? 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,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user