Skip to content

Commit 60b447e

Browse files
committed
feat: add syntax highlighting
1 parent cbb0b34 commit 60b447e

File tree

6 files changed

+94
-8
lines changed

6 files changed

+94
-8
lines changed

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
<link
88
rel="preload"
9-
href="https://unpkg.com/highlight.js@11.0.1/styles/ascetic.css"
9+
href="https://unpkg.com/highlight.js@11.1.0/styles/atom-one-light.css"
1010
as="style"
1111
onload="this.onload=null;this.rel='stylesheet'"
1212
/>
1313
<noscript
1414
><link
1515
rel="stylesheet"
16-
href="https://unpkg.com/highlight.js@11.0.1/styles/ascetic.css"
16+
href="https://unpkg.com/highlight.js@11.1.0/styles/atom-one-light.css"
1717
/></noscript>
1818
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
1919
<title>Mark</title>

postcss.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ module.exports = {
88
require("tailwindcss"),
99
require("autoprefixer"),
1010
],
11+
safelist: {
12+
deep: [/hljs-/],
13+
},
1114
};

src/lib/marked/helpers.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copied from https://github.com/markedjs/marked/blob/v3.0.2/src/helpers.js
2+
3+
const escapeTest = /[&<>"']/;
4+
const escapeReplace = /[&<>"']/g;
5+
const escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/;
6+
const escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g;
7+
const escapeReplacements = {
8+
"&": "&amp;",
9+
"<": "&lt;",
10+
">": "&gt;",
11+
'"': "&quot;",
12+
"'": "&#39;",
13+
};
14+
const getEscapeReplacement = (ch) => escapeReplacements[ch];
15+
16+
function escape(html, encode) {
17+
if (encode) {
18+
if (escapeTest.test(html)) {
19+
return html.replace(escapeReplace, getEscapeReplacement);
20+
}
21+
} else {
22+
if (escapeTestNoEncode.test(html)) {
23+
return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
24+
}
25+
}
26+
27+
return html;
28+
}
29+
30+
export { escape };

src/lib/marked/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import marked from "marked";
2+
import { escape } from "./helpers";
3+
import hljs from "highlight.js";
4+
5+
marked.setOptions({
6+
highlight: function (code, lang, callback) {
7+
console.log({ code, lang, callback });
8+
const highlightedCode = hljs.highlight(code, { language: lang }).value;
9+
return highlightedCode;
10+
},
11+
});
12+
13+
const renderer = {
14+
code(code, infostring, escaped) {
15+
const lang = (infostring || "").match(/\S*/)[0];
16+
if (this.options.highlight) {
17+
const out = this.options.highlight(code, lang);
18+
if (out != null && out !== code) {
19+
escaped = true;
20+
code = out;
21+
}
22+
}
23+
24+
code = code.replace(/\n$/, "") + "\n";
25+
26+
if (!lang) {
27+
return (
28+
"<pre class='codeblock'" +
29+
(escaped ? code : escape(code, true)) +
30+
"</pre>\n"
31+
);
32+
}
33+
34+
return (
35+
'<pre class="codeblock ' +
36+
this.options.langPrefix +
37+
escape(lang, true) +
38+
'">' +
39+
(escaped ? code : escape(code, true)) +
40+
"</pre>\n"
41+
);
42+
},
43+
};
44+
45+
marked.use({ renderer });
46+
47+
export default marked;

src/pages/Home.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import Toast from "../components/toast.vue";
6565
import { copy } from "../lib/copy";
6666
import { defaultMarkdownText } from "../resources/default-md";
6767
import { reactive, onMounted, ref, onUnmounted } from "vue";
68-
import marked from "marked";
68+
import marked from "../lib/marked";
6969
7070
const toastRef = ref(null);
7171

tailwind.config.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
module.exports = {
2-
purge: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],
2+
purge: {
3+
enabled: true,
4+
content: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
5+
safelist: {
6+
deep: [/hljs-/],
7+
},
8+
},
39
darkMode: false, // or 'media' or 'class'
410
theme: {
5-
extend: {}
11+
extend: {},
612
},
713
variants: {
8-
extend: {}
14+
extend: {},
915
},
10-
plugins: []
11-
}
16+
plugins: [],
17+
};

0 commit comments

Comments
 (0)