From c9b43ea32b13dc36385022dd00a34f7e9cc7baef Mon Sep 17 00:00:00 2001 From: Christian Svensson Date: Thu, 31 Jul 2025 23:48:00 +0200 Subject: [PATCH] feat(docs): add file/language type icons to code blocks (#3162) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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
Open — Before | After ### TS + Shell icons - Hover tooltips and alt text image ### TS + Shell icons 2 image ### Shell + Dockerfile icons image ### Nested blocks + Git icon image
--- docs/canary/concepts/static-files.md | 6 +- docs/latest/concepts/server-configuration.md | 2 +- docs/latest/concepts/static-files.md | 4 +- docs/latest/examples/rendering-markdown.md | 2 +- .../latest/getting-started/running-locally.md | 2 +- www/static/logos/css.svg | 1 + www/static/logos/diff.svg | 1 + www/static/logos/docker.svg | 1 + www/static/logos/git.svg | 1 + www/static/logos/html.svg | 1 + www/static/logos/json.svg | 1 + www/static/logos/markdown.svg | 1 + www/static/logos/shell.svg | 1 + www/static/logos/text.svg | 1 + www/static/markdown.css | 9 +++ www/utils/markdown.ts | 79 ++++++++++++++++++- 16 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 www/static/logos/css.svg create mode 100644 www/static/logos/diff.svg create mode 100644 www/static/logos/docker.svg create mode 100644 www/static/logos/git.svg create mode 100644 www/static/logos/html.svg create mode 100644 www/static/logos/json.svg create mode 100644 www/static/logos/markdown.svg create mode 100644 www/static/logos/shell.svg create mode 100644 www/static/logos/text.svg diff --git a/docs/canary/concepts/static-files.md b/docs/canary/concepts/static-files.md index 83c1b1e9..3bd88973 100644 --- a/docs/canary/concepts/static-files.md +++ b/docs/canary/concepts/static-files.md @@ -30,7 +30,7 @@ const builder = new Builder({ staticDir: "path/to/staticDir" }); By default, Fresh adds caching headers for the `src` and `srcset` attributes on `` and `` tags. -```jsx +```tsx // Caching headers will be automatically added ; ``` @@ -38,7 +38,7 @@ By default, Fresh adds caching headers for the `src` and `srcset` attributes on You can always opt out of this behaviour per tag, by adding the `data-fresh-disable-lock` attribute. -```jsx +```tsx // Opt-out of automatic caching headers ; ``` @@ -48,7 +48,7 @@ You can always opt out of this behaviour per tag, by adding the Use the `asset()` function to add caching headers manually. It will be served with a cache lifetime of one year. -```jsx +```tsx import { asset } from "fresh/runtime"; // Adding caching headers manually diff --git a/docs/latest/concepts/server-configuration.md b/docs/latest/concepts/server-configuration.md index 7e6a35b8..3b162aa7 100644 --- a/docs/latest/concepts/server-configuration.md +++ b/docs/latest/concepts/server-configuration.md @@ -151,7 +151,7 @@ The `basePath` will be automatically applied to absolute links in your app. For example, when the `basePath` is `/foo/bar`, linking to `/about` will automatically become `/foo/bar/about`. -```jsx +```tsx About; ``` diff --git a/docs/latest/concepts/static-files.md b/docs/latest/concepts/static-files.md index b3c96d7a..880de300 100644 --- a/docs/latest/concepts/static-files.md +++ b/docs/latest/concepts/static-files.md @@ -29,7 +29,7 @@ version of this path that contains a build ID for cache busting. When the asset is requested at this "locked" path, it will be served with a cache lifetime of one year. -```jsx routes/page.tsx +```tsx routes/page.tsx import { asset } from "$fresh/runtime.ts"; export default function Page() { @@ -46,6 +46,6 @@ and `` HTML tags. These will automatically use "locked" paths if Fresh deems it safe to do so. You can always opt out of this behaviour per tag, by adding the `data-fresh-disable-lock` attribute. -```jsx +```tsx ; ``` diff --git a/docs/latest/examples/rendering-markdown.md b/docs/latest/examples/rendering-markdown.md index 1150b579..02d28b37 100644 --- a/docs/latest/examples/rendering-markdown.md +++ b/docs/latest/examples/rendering-markdown.md @@ -92,7 +92,7 @@ description: testFromText You'll also need to import the `Github Flavored Markdown` module: -```bash +```sh deno add jsr:@deno/gfm ``` diff --git a/docs/latest/getting-started/running-locally.md b/docs/latest/getting-started/running-locally.md index a8299669..9dbedce0 100644 --- a/docs/latest/getting-started/running-locally.md +++ b/docs/latest/getting-started/running-locally.md @@ -38,7 +38,7 @@ you add a new route. If you want to change the port or host, modify the config bag of the `start()` call in `main.ts` to include an explicit port number: -```js main.ts +```ts main.ts await start(manifest, { server: { port: 3000 } }); ``` diff --git a/www/static/logos/css.svg b/www/static/logos/css.svg new file mode 100644 index 00000000..13691bdb --- /dev/null +++ b/www/static/logos/css.svg @@ -0,0 +1 @@ +CSS Logo diff --git a/www/static/logos/diff.svg b/www/static/logos/diff.svg new file mode 100644 index 00000000..7e22809e --- /dev/null +++ b/www/static/logos/diff.svg @@ -0,0 +1 @@ +file_type_diff \ No newline at end of file diff --git a/www/static/logos/docker.svg b/www/static/logos/docker.svg new file mode 100644 index 00000000..98727887 --- /dev/null +++ b/www/static/logos/docker.svg @@ -0,0 +1 @@ +file_type_docker2 \ No newline at end of file diff --git a/www/static/logos/git.svg b/www/static/logos/git.svg new file mode 100644 index 00000000..c5ea394b --- /dev/null +++ b/www/static/logos/git.svg @@ -0,0 +1 @@ +file_type_git \ No newline at end of file diff --git a/www/static/logos/html.svg b/www/static/logos/html.svg new file mode 100644 index 00000000..a0152d86 --- /dev/null +++ b/www/static/logos/html.svg @@ -0,0 +1 @@ +file_type_html \ No newline at end of file diff --git a/www/static/logos/json.svg b/www/static/logos/json.svg new file mode 100644 index 00000000..26c39ba7 --- /dev/null +++ b/www/static/logos/json.svg @@ -0,0 +1 @@ +file_type_json \ No newline at end of file diff --git a/www/static/logos/markdown.svg b/www/static/logos/markdown.svg new file mode 100644 index 00000000..c5b32a6f --- /dev/null +++ b/www/static/logos/markdown.svg @@ -0,0 +1 @@ +file_type_markdown \ No newline at end of file diff --git a/www/static/logos/shell.svg b/www/static/logos/shell.svg new file mode 100644 index 00000000..17d38213 --- /dev/null +++ b/www/static/logos/shell.svg @@ -0,0 +1 @@ +file_type_shell \ No newline at end of file diff --git a/www/static/logos/text.svg b/www/static/logos/text.svg new file mode 100644 index 00000000..a5562edd --- /dev/null +++ b/www/static/logos/text.svg @@ -0,0 +1 @@ +file_type_text \ No newline at end of file diff --git a/www/static/markdown.css b/www/static/markdown.css index 87c953d1..4d30ead0 100644 --- a/www/static/markdown.css +++ b/www/static/markdown.css @@ -812,11 +812,20 @@ ol.nested li:before { border-top-right-radius: 0 !important; } .fenced-code-title { + display: grid; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 0.8125rem; line-height: 1; + grid-auto-flow: column; + align-items: center; + gap: 0.625rem; +} +.fenced-code-title > img { + background-color: transparent; + margin: -0.2rem 0; + width: 1.25rem; } .highlight-source-yml .atrule { diff --git a/www/utils/markdown.ts b/www/utils/markdown.ts index 2af3367a..e6ee0122 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 { 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 = `
`; - if (title) { + if (title || icon) { + const image = icon + ? `${icon.text}` + : ""; + out += `
+ ${image} ${title ? escapeHtml(String(title)) : " "}
`; @@ -139,9 +212,9 @@ class DefaultRenderer extends Marked.Renderer { const icon = ``; return `
\n${icon}${ label[type] - }${Marked.parser(tokens)}
\n`; + }${this.parser.parse(tokens)}\n`; } - return `
\n${Marked.parser(tokens)}
\n`; + return `
\n${this.parser.parse(tokens)}
\n`; } }