www: new fenced code block design (#1629)

This commit is contained in:
Marvin Hagemeister
2023-08-13 10:52:41 +02:00
committed by GitHub
parent 7679e278cc
commit 6a599dbed5
43 changed files with 363 additions and 342 deletions
+1 -2
View File
@@ -14,8 +14,7 @@
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3",
"$std/": "https://deno.land/std@0.193.0/",
"$ga4": "https://raw.githubusercontent.com/denoland/ga4/main/mod.ts",
"$marked-mangle": "https://esm.sh/marked-mangle@1.0.1",
"$marked-smartypants": "https://esm.sh/marked-smartypants@1.0.1"
"$marked-mangle": "https://esm.sh/marked-mangle@1.0.1"
},
"compilerOptions": {
"jsx": "react-jsx",
+2 -7
View File
@@ -10,13 +10,8 @@ import {
TABLE_OF_CONTENTS,
TableOfContentsEntry,
} from "../../data/docs.ts";
import { frontMatter, gfm } from "../../utils/markdown.ts";
import { frontMatter, renderMarkdown } from "../../utils/markdown.ts";
import toc from "../../../docs/toc.ts";
import { mangle } from "$marked-mangle";
import { markedSmartypants } from "$marked-smartypants";
gfm.Marked.marked.use(mangle());
gfm.Marked.marked.use(markedSmartypants());
interface Data {
page: Page;
@@ -249,7 +244,7 @@ function DesktopSidebar(props: { path: string; page: Page }) {
}
function Content(props: { page: Page }) {
const html = gfm.render(props.page.markdown);
const html = renderMarkdown(props.page.markdown);
return (
<main class="py-6 overflow-hidden md:mr-4 lg:mr-32">
<h1 class="text(4xl gray-900) tracking-tight font-extrabold mt-6 md:mt-0">
+39 -1
View File
@@ -3,8 +3,20 @@ import { gfm } from "../utils/markdown.ts";
// TODO(lucacasonato): hash the file and use the hash as the filename, and serve
// with high-cacheability headers.
function css(template: TemplateStringsArray, ...params: string[]) {
let out = "";
const CSS = `${gfm.CSS}
for (let i = 0; i < template.length; i++) {
out += template[i];
if (i < params.length) {
out += String(params[i]);
}
}
return out;
}
const CSS = css`${gfm.CSS}
ol.nested {
counter-reset: item;
@@ -39,6 +51,32 @@ ol.nested li:before {
.toggle:checked + .toggled {
display: block;
}
.fenced-code {
margin-bottom: 1rem;
}
.fenced-code pre {
margin-bottom: 0;
}
.fenced-code-header {
border-top-left-radius: .5rem;
border-top-right-radius: .5rem;
padding: 0.5rem;
background: #eaeef1;
display: flex;
align-items: center;
justify-content: space-between;
}
.fenced-code-header + pre {
border-top-left-radius: 0 !important;
border-top-right-radius: 0 !important;
}
.fenced-code-title {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", "Courier New", monospace;
font-size: .8125rem;
line-height: 1;
}
`;
export const handler: Handlers = {
+111
View File
@@ -3,5 +3,116 @@ import "https://esm.sh/prismjs@1.29.0/components/prism-jsx.js?no-check";
import "https://esm.sh/prismjs@1.29.0/components/prism-typescript.js?no-check";
import "https://esm.sh/prismjs@1.29.0/components/prism-tsx.js?no-check";
import "https://esm.sh/prismjs@1.29.0/components/prism-diff.js?no-check";
import "https://esm.sh/prismjs@1.29.0/components/prism-json.js?no-check";
import "https://esm.sh/prismjs@1.29.0/components/prism-bash.js?no-check";
export { extract as frontMatter } from "$std/front_matter/yaml.ts";
import Prism from "https://esm.sh/prismjs@1.29.0";
import * as Marked from "https://esm.sh/marked@7.0.2";
import { escape as escapeHtml } from "$std/html/entities.ts";
import { mangle } from "$marked-mangle";
Marked.marked.use(mangle());
class DefaultRenderer extends Marked.Renderer {
text(text: string): string {
// Smartypants typography enhancement
return 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;");
}
heading(
text: string,
level: 1 | 2 | 3 | 4 | 5 | 6,
raw: string,
slugger: Marked.Slugger,
): string {
const slug = slugger.slug(raw);
return `<h${level} id="${slug}"><a class="anchor" aria-hidden="true" tabindex="-1" href="#${slug}">#</a>${text}</h${level}>`;
}
link(href: string, title: string | null, text: string) {
const titleAttr = title ? ` title="${title}"` : "";
if (href.startsWith("#")) {
return `<a href="${href}"${titleAttr}>${text}</a>`;
}
if (this.options.baseUrl) {
try {
href = new URL(href, this.options.baseUrl).href;
} catch (_) {
//
}
}
return `<a href="${href}"${titleAttr} rel="noopener noreferrer">${text}</a>`;
}
image(src: string, title: string | null, alt: string | null) {
return `<img src="${src}" alt="${alt ?? ""}" title="${title ?? ""}" />`;
}
code(code: string, info: string | undefined): string {
// 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] ?? "";
}
let out = `<div class="fenced-code">`;
if (title) {
out += `<div class="fenced-code-header">
<span class="fenced-code-title lang-${lang}">
${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(code)}</code></pre>`;
} else {
const html = Prism.highlight(code, grammar, lang);
out +=
`<pre class="highlight highlight-source-${lang} notranslate lang-${lang}">${html}</pre>`;
}
out += `</div>`;
return out;
}
}
export interface MarkdownOptions {
inline?: boolean;
}
export function renderMarkdown(
input: string,
opts: MarkdownOptions = {},
): string {
const markedOpts: Marked.MarkedOptions = {
gfm: true,
renderer: new DefaultRenderer(),
};
const html = opts.inline
? Marked.parseInline(input, markedOpts) as string
: Marked.parse(input, markedOpts) as string;
return html;
}