-
Notifications
You must be signed in to change notification settings - Fork 7
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
arily
committed
Jul 7, 2023
1 parent
e6de7ee
commit 473c82e
Showing
11 changed files
with
150 additions
and
150 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<script setup lang="ts"> | ||
import { type JSONContent } from '@tiptap/vue-3' | ||
const props = defineProps<{ | ||
json?: JSONContent | ||
html: string | ||
}>() | ||
const html = shallowRef(props.html) | ||
const el = shallowRef<HTMLElement>() | ||
onBeforeMount(hl) | ||
watch(() => props.html, hl) | ||
async function hl() { | ||
if (process.server) { | ||
return props.html | ||
} | ||
const _hljs = await import('highlight.js/lib/core').then(m => m.default) | ||
const hljs = _hljs.newInstance() | ||
const libs = await parseAndImportHighlightLibFromHtml(props.html) as any[] | ||
libs.forEach(([lKey, lib]) => { | ||
if (!hljs.listLanguages().includes(lKey)) { | ||
hljs.registerLanguage(lKey, lib) | ||
} | ||
}) | ||
el.value = document.createElement('div') | ||
el.value.innerHTML = props.html | ||
el.value.querySelectorAll('pre code').forEach((cb) => { | ||
const language = [...cb.classList].find(i => i.startsWith('language-'))?.slice('language-'.length) | ||
if (!language) { | ||
return | ||
} | ||
cb.innerHTML = hljs.highlight(language, (cb as HTMLElement).innerText).value | ||
}) | ||
html.value = el.value.innerHTML | ||
} | ||
</script> | ||
|
||
<template> | ||
<div | ||
class="custom-typography" | ||
v-html="html" | ||
/> | ||
</template> | ||
|
||
<style src="@/components/content/styles/typography.scss" lang="scss"></style> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { JSONContent } from '@tiptap/core' | ||
import { lowlight } from 'lowlight/lib/core.js' | ||
import allLanguages from '~~/configs/hljs' | ||
|
||
export type LanguageKey = keyof typeof allLanguages | ||
export type Language = typeof allLanguages[LanguageKey] | ||
|
||
export async function importHighlightLanguage(language: Language) { | ||
return await import(`../../node_modules/highlight.js/es/languages/${language}.js`) | ||
} | ||
|
||
export async function useLowlightLanguage(language: Language) { | ||
try { | ||
const f = await importHighlightLanguage(language) | ||
lowlight.registerLanguage(language, f.default) | ||
} | ||
catch (e) { | ||
console.error('error on loading hljs lib:', e) | ||
} | ||
} | ||
export async function parseAndImportHighlightLibFromHtml(html: string) { | ||
const languages = getLanguagesFromHTML(html) | ||
return Promise.all(languages.map(async lKey => [lKey, await importHighlightLanguage(allLanguages[lKey]).then(v => v.default)])) | ||
} | ||
|
||
export function getLanguagesFromHTML(html: string) { | ||
const matchedLanguages = html.matchAll(/language-(\w+)/gm) | ||
const languages = [] as LanguageKey[] | ||
for (const _language of matchedLanguages) { | ||
const language = _language[1] as LanguageKey | ||
if (!allLanguages[language]) { | ||
continue | ||
} | ||
languages.push(_language[1] as LanguageKey) | ||
} | ||
return languages | ||
} | ||
export function loadAllLanguagesInJSONContent(json: JSONContent) { | ||
return json.content | ||
? Promise.allSettled(json.content?.map((node) => { | ||
if (node.type !== 'codeBlock') { | ||
return undefined | ||
} | ||
|
||
const language = node.attrs?.language as LanguageKey | undefined | ||
if (!language) { | ||
return undefined | ||
} | ||
|
||
if (lowlight.registered(language)) { | ||
return undefined | ||
} | ||
|
||
if (!allLanguages[language]) { | ||
return undefined | ||
} | ||
|
||
return useLowlightLanguage(allLanguages[language]) | ||
})).then(noop) | ||
: Promise.resolve() | ||
} |
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