feat(docs): add file/language type icons to code blocks (#3162)

I originally wanted to make it clearer the distinction between "files"
and e.g. `` ```tsx foo.tsx `` compared to `` ```sh Terminal `` or ``
```txt Project structure``. I originally started just trying to add a 📝
icon or something. But instead I ended showing file types of all
markdown languages.

I took the file type icons for the SVG's from
https://github.com/vscode-icons/vscode-icons, which has a MIT license.

I think the icons make it a even more clear what the language of the
code snippet is.

I have a follow-up PR for improving code block titles and making
examples more consistent in #3163.


## Comparisons

<details>

<summary><strong>Open &mdash; Before | After</strong></summary>


### TS + Shell icons - Hover tooltips and alt text

<img width="1504" height="639" alt="image"
src="https://github.com/user-attachments/assets/636838a3-f38c-4048-a536-52c1cdd6b6d4"
/>


### TS + Shell icons 2

<img width="1511" height="761" alt="image"
src="https://github.com/user-attachments/assets/2ad6d25c-3995-433a-ad93-162830ca55a1"
/>

### Shell + Dockerfile icons

<img width="1500" height="780" alt="image"
src="https://github.com/user-attachments/assets/88010749-d0d2-4498-9eb2-9bd971e48365"
/>

### Nested blocks + Git icon

<img width="1513" height="634" alt="image"
src="https://github.com/user-attachments/assets/400c27f8-d0b4-40bd-801d-611e1e8a8e36"
/>

</details>
This commit is contained in:
Christian Svensson
2025-07-31 23:48:00 +02:00
committed by GitHub
parent 62f8bb008e
commit c9b43ea32b
16 changed files with 102 additions and 11 deletions
+76 -3
View File
@@ -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 { asset } from "fresh/runtime";
import { escape as escapeHtml } from "@std/html";
import { mangle } from "marked-mangle";
import GitHubSlugger from "github-slugger";
@@ -14,6 +15,69 @@ 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$/,
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;
@@ -95,11 +159,20 @@ class DefaultRenderer extends Marked.Renderer {
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) {
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>`;
@@ -139,9 +212,9 @@ class DefaultRenderer extends Marked.Renderer {
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>${Marked.parser(tokens)}</blockquote>\n`;
}</span>${this.parser.parse(tokens)}</blockquote>\n`;
}
return `<blockquote>\n${Marked.parser(tokens)}</blockquote>\n`;
return `<blockquote>\n${this.parser.parse(tokens)}</blockquote>\n`;
}
}