Skip to content

Commit

Permalink
refactor: code highlighting (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppvg authored Jan 2, 2024
1 parent 655a442 commit 8802dee
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 37 deletions.
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"dependencies": {
"@minvws/manon": "workspace:*",
"@minvws/manon-themes": "workspace:*"
"@minvws/manon-themes": "workspace:*",
"highlight.js": "^11.9.0"
},
"devDependencies": {
"@sveltejs/adapter-static": "^2.0.3",
Expand All @@ -24,7 +25,6 @@
"sass": "^1.69.5",
"svelte": "^4.2.8",
"svelte-check": "^3.6.2",
"svelte-highlight": "^7.4.2",
"svelte-preprocess-import-assets": "^1.1.0",
"typescript": "^5.3.3",
"vite": "^4.5.1"
Expand Down
42 changes: 29 additions & 13 deletions docs/src/lib/Code.svelte
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
<script context="module">
import hljs from "highlight.js/lib/core";
import xml from "highlight.js/lib/languages/xml";
import css from "highlight.js/lib/languages/css";
import scss from "highlight.js/lib/languages/scss";
import plaintext from "highlight.js/lib/languages/plaintext";
hljs.registerLanguage("html", xml);
hljs.registerLanguage("css", css);
hljs.registerLanguage("scss", scss);
hljs.registerLanguage("plaintext", plaintext);
</script>

<script>
import Highlight from "svelte-highlight";
import xml from "svelte-highlight/languages/xml";
import css from "svelte-highlight/languages/css";
import scss from "svelte-highlight/languages/scss";
import plaintext from "svelte-highlight/languages/plaintext";
import "svelte-highlight/styles/github.css";
import "highlight.js/styles/github.css";
const lang = { html: xml, css, scss, plaintext };
const trim = (markup) => markup.replace(/^(\s*\n)+/, "").replace(/\n\s*$/, "");
/** @type {'html' | 'css' | 'scss' | 'plaintext'} */
export let language = "html";
export let language = "plaintext";
export let code = "";
const trimPattern = /^\s+|\s+$/g;
$: trimmed = code.replace(trimPattern, "");
$: trimmed = trim(code);
$: highlighted = hljs.highlight(trimmed, { language }).value;
</script>

<Highlight language={lang[language] || plaintext} code={trimmed} />
{@html `<!--
${trimmed}
-->`}
<pre><code>{@html highlighted}</code></pre>

<style>
code {
padding: 1rem;
overflow-x: auto;
background-color: #ffffff;
}
</style>
4 changes: 2 additions & 2 deletions docs/src/routes/(docs)/components/iconset-add/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@
.icon,
.icon-only {
&amp;:before {
&:before {
@include icon;
}
&amp;.icon-cat:before {
&.icon-cat:before {
content: var(--icon-cat);
font-family: "Manon icons";
}
Expand Down
20 changes: 10 additions & 10 deletions docs/src/routes/(docs)/components/iconset-create/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@
.icon,
.icon-only {
&amp;:before {
&:before {
@include icon;
}
&amp;.icon-cat:before {
&.icon-cat:before {
content: var(--icon-cat);
font-family: "Manon icons";
}
Expand All @@ -136,11 +136,11 @@
code={`
.icon,
.icon-only {
&amp;:before {
&:before {
@include icon;
}
&amp;.icon-cat:before {
&.icon-cat:before {
content: var(--icon-cat);
font-family: "Manon icons";
}
Expand All @@ -155,11 +155,11 @@
code={`
.icon,
.icon-only {
&amp;:after {
&:after {
@include icon;
}
&amp;.icon-cat:after {
&.icon-cat:after {
content: var(--icon-cat);
font-family: "Manon icons";
}
Expand All @@ -179,13 +179,13 @@
code={`
.icon,
.icon-only {
&amp;:before,
&amp;:after {
&:before,
&:after {
@include icon;
}
&amp;.icon-cat-before:before,
&amp;.icon-cat-after:after {
&.icon-cat-before:before,
&.icon-cat-after:after {
content: var(--icon-cat);
font-family: "Manon icons";
}
Expand Down
14 changes: 4 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8802dee

Please sign in to comment.