Skip to content

Commit b33c1b3

Browse files
Allow titles for code blocks
1 parent f4ae669 commit b33c1b3

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
cov_profile
2-
deno.lock
2+
deno.lock
3+
.DS_Store

mod.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,16 @@ export class Renderer extends Marked.Renderer {
5353
}
5454

5555
code(code: string, language?: string): string {
56-
// a language of `ts, ignore` should really be `ts`
57-
// and it should be lowercase to ensure it has parity with regular github markdown
58-
language = language?.split(",")?.[0].toLocaleLowerCase();
56+
const isTitleIncluded = language?.match(/\stitle="(.+)"/);
57+
let title = null;
58+
if (isTitleIncluded) {
59+
language = language!.split(" ")[0];
60+
title = isTitleIncluded[1];
61+
} else {
62+
// a language of `ts, ignore` should really be `ts`
63+
// and it should be lowercase to ensure it has parity with regular github markdown
64+
language = language?.split(",")?.[0].toLocaleLowerCase();
65+
}
5966

6067
// transform math code blocks into HTML+MathML
6168
// https://github.blog/changelog/2022-06-28-fenced-block-syntax-for-mathematical-expressions/
@@ -70,7 +77,10 @@ export class Renderer extends Marked.Renderer {
7077
return `<pre><code class="notranslate">${he.encode(code)}</code></pre>`;
7178
}
7279
const html = Prism.highlight(code, grammar, language!);
73-
return `<div class="highlight highlight-source-${language} notranslate"><pre>${html}</pre></div>`;
80+
const titleHtml = title
81+
? `<div class="markdown-code-title">${title}</div>`
82+
: ``;
83+
return `<div class="highlight highlight-source-${language} notranslate">${titleHtml}<pre>${html}</pre></div>`;
7484
}
7585

7686
link(href: string, title: string | null, text: string): string {

0 commit comments

Comments
 (0)