Skip to content

Commit b5bf95d

Browse files
authored
Merge pull request #12 from Syrup/fix/clean-urls-and-code-labels
Fix: Clean URLs and code block language labels
2 parents 122d323 + e1bf2f7 commit b5bf95d

5 files changed

Lines changed: 61 additions & 23 deletions

File tree

cli/build.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,21 @@ import { readFileSync, writeFileSync, readdirSync, existsSync, rmSync, mkdirSync
22
import { join, dirname } from 'node:path';
33
import { glob } from 'glob';
44
import frontMatter from 'front-matter';
5-
import hljs from 'highlight.js';
65
import Handlebars from 'handlebars';
76
import ejs from 'ejs';
87
import MarkdownIt from 'markdown-it';
98
import markdownItTaskLists from 'markdown-it-task-lists';
109
import markdownItEmoji from 'markdown-it-emoji/light.js';
1110
import markdownItAlerts from '../helpers/markdown-it-alerts.js';
11+
import { highlightCode } from '../helpers/highlight.js';
1212
import { parse as parseToml } from 'toml';
1313
import '../helpers/index.js';
1414

1515
const configFile = readFileSync(join(process.cwd(), 'config.toml'), 'utf8');
1616
const config = parseToml(configFile);
1717

1818
const md = new MarkdownIt({
19-
highlight: function (str, lang) {
20-
if (lang && hljs.getLanguage(lang)) {
21-
try {
22-
return '<pre class="hljs"><code>' +
23-
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
24-
'</code></pre>';
25-
} catch (__) {}
26-
}
27-
return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
28-
},
19+
highlight: highlightCode,
2920
html: true
3021
});
3122

helpers/highlight.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import hljs from 'highlight.js';
2+
import MarkdownIt from 'markdown-it';
3+
4+
const mdUtils = MarkdownIt().utils;
5+
6+
export function highlightCode(str, lang) {
7+
if (lang && hljs.getLanguage(lang)) {
8+
try {
9+
const langLabel = `<span class="code-lang">${mdUtils.escapeHtml(lang)}</span>`;
10+
return langLabel +
11+
`<pre class="hljs" data-lang="${mdUtils.escapeHtml(lang)}">` +
12+
`<code>` +
13+
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
14+
'</code></pre>';
15+
} catch (__) {}
16+
}
17+
return '<pre class="hljs"><code>' + mdUtils.escapeHtml(str) + '</code></pre>';
18+
}

index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { readFileSync } from 'node:fs';
33
import { join } from 'node:path';
44
import { glob } from 'glob';
55
import frontMatter from 'front-matter';
6-
import hljs from 'highlight.js';
76
import express from 'express';
87
import Handlebars from 'handlebars';
98
import MarkdownIt from 'markdown-it';
109
import markdownItTaskLists from 'markdown-it-task-lists';
1110
import markdownItEmoji from 'markdown-it-emoji/light.js';
1211
import markdownItAlerts from './helpers/markdown-it-alerts.js';
12+
import { highlightCode } from './helpers/highlight.js';
1313
import { parse as parseToml } from 'toml';
1414
import './helpers/index.js';
1515

@@ -24,16 +24,7 @@ const config = parseToml(configFile);
2424

2525
// Setup markdown-it
2626
const md = new MarkdownIt({
27-
highlight: function (str, lang) {
28-
if (lang && hljs.getLanguage(lang)) {
29-
try {
30-
return '<pre class="hljs"><code>' +
31-
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
32-
'</code></pre>';
33-
} catch (__) {}
34-
}
35-
return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
36-
},
27+
highlight: highlightCode,
3728
html: true
3829
});
3930

public/style.css

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ pre.hljs {
5151
position: relative;
5252
}
5353

54+
pre.hljs code {
55+
display: block;
56+
position: relative;
57+
}
58+
59+
pre.hljs .code-lang {
60+
position: sticky;
61+
top: 0;
62+
left: 0;
63+
float: left;
64+
background-color: transparent;
65+
color: #888;
66+
padding: 2px 6px;
67+
font-size: 9px;
68+
font-family: 'Courier New', monospace;
69+
font-weight: 600;
70+
line-height: 1;
71+
text-transform: none;
72+
border-top-left-radius: 2px;
73+
border-bottom-right-radius: 3px;
74+
user-select: none;
75+
pointer-events: none;
76+
margin: 0;
77+
margin-bottom: -14px;
78+
display: inline-block;
79+
z-index: 1;
80+
}
81+
5482
.code-wrapper {
5583
position: relative;
5684
display: inline-block;
@@ -264,6 +292,11 @@ blockquote.alert p:last-child {
264292
margin-bottom: 0;
265293
}
266294

295+
:root.dark .code-lang {
296+
background-color: transparent !important;
297+
color: #666 !important;
298+
}
299+
267300
@media screen and (max-width: 600px) {
268301
pre.hljs,
269302
.code-wrapper {

vercel.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
"buildCommand": "npm run build",
33
"outputDirectory": "dist",
44
"devCommand": "npm run dev",
5-
"installCommand": "npm install"
5+
"installCommand": "npm install",
6+
"cleanUrls": true,
7+
"trailingSlash": false,
8+
"rewrites": [
9+
{ "source": "/((?!.*\\.).*)", "destination": "/$1.html" }
10+
]
611
}

0 commit comments

Comments
 (0)