-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
179 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,21 +82,51 @@ | |
</script> | ||
<script type='module'> | ||
import { codeToHtml } from 'https://esm.sh/[email protected]' | ||
// Our old <code> element will be replaced by the one created by Shiki, | ||
// we need to keep old class names and copy to the new one. | ||
function getShikiOpt(lang, classes) { | ||
return { | ||
lang, | ||
theme: 'one-dark-pro', | ||
// Ref: https://shiki.style/guide/transformers#transformer-hooks | ||
transformers: [ | ||
{ | ||
code(node) { | ||
classes.forEach((c) => this.addClassToHast(node, c)) | ||
}, | ||
pre(node) { | ||
if (!classes.includes('q-with-lineno')) { | ||
this.addClassToHast(node, 'p-4') | ||
} | ||
}, | ||
} | ||
] | ||
} | ||
} | ||
document.addEventListener('alpine:init', () => { | ||
Alpine.data('need_highlight', () => ({ | ||
code: '', | ||
classes: [], | ||
lang: 'text', | ||
init() { | ||
const codeElm = this.$refs.orig_code; | ||
const code = codeElm.textContent | ||
this.code = code.trim() | ||
let className = Array.from(codeElm.classList.values()).find((c) => c.startsWith('language-')) | ||
let classes = Array.from(codeElm.classList.values()) | ||
let className = classes.find((c) => c.startsWith('language-')) | ||
if (className) { | ||
this.lang = className.split('-')[1] | ||
} | ||
this.classes = classes | ||
}, | ||
async highlight() { | ||
const html = await codeToHtml(this.code, { lang: this.lang, theme: 'one-dark-pro' }) | ||
const lang = this.lang | ||
const classes = this.classes | ||
const opts = getShikiOpt(lang, classes) | ||
const html = await codeToHtml(this.code, opts) | ||
return html | ||
} | ||
})) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters