This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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
8 changed files
with
108 additions
and
7 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
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,20 @@ | ||
// Based on https://docs.mathjax.org/en/latest/web/configuration.html#configuring-and-loading-in-one-script | ||
window.MathJax = { | ||
tex: { | ||
inlineMath: [["$", "$"]], | ||
displayMath: [["$$", "$$"]], | ||
}, | ||
svg: { | ||
fontCache: "global", | ||
}, | ||
}; | ||
|
||
function loadMathJax() { | ||
var script = document.createElement("script"); | ||
script.src = "https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js"; | ||
// script.src = "https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-mml-chtml.js"; | ||
script.async = true; | ||
document.head.appendChild(script); | ||
} | ||
|
||
loadMathJax(); |
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,26 @@ | ||
import { unref, onMounted, onUpdated } from "vue"; | ||
import type { MaybeRef } from "vue"; | ||
|
||
export function useMathJax(element: MaybeRef<HTMLElement | undefined>) { | ||
onMounted(() => { | ||
typeset(); | ||
}); | ||
|
||
onUpdated(() => { | ||
typeset(); | ||
}); | ||
|
||
function typeset() { | ||
// @ts-ignore | ||
if (!window.MathJax) return; | ||
|
||
const ele = unref(element); | ||
if (!ele) return; | ||
try { | ||
// @ts-ignore | ||
window.MathJax.typesetPromise([ele]); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,20 +1,45 @@ | ||
import { computed, toValue } from "vue"; | ||
import type { MaybeRefOrGetter } from "vue"; | ||
import { computed, unref, toValue } from "vue"; | ||
import type { ComputedRef, MaybeRef, MaybeRefOrGetter } from "vue"; | ||
import { Marked } from "marked"; | ||
import { markedHighlight } from "marked-highlight"; | ||
import hljs from "highlight.js"; | ||
import $ from "cash-dom"; | ||
|
||
const marked = new Marked( | ||
markedHighlight({ | ||
const marked = new Marked({ | ||
gfm: true, | ||
...markedHighlight({ | ||
langPrefix: "hljs language-", | ||
highlight(code, lang) { | ||
const language = hljs.getLanguage(lang) ? lang : "plaintext"; | ||
return hljs.highlight(code, { language }).value; | ||
}, | ||
}) | ||
); | ||
}), | ||
}); | ||
|
||
export function useParsed(markdown: MaybeRefOrGetter<string>) { | ||
const parsed = computed(() => marked.parse(toValue(markdown))); | ||
const _parsed = computed(() => | ||
marked.parse(toValue(markdown)) | ||
) as ComputedRef<string>; | ||
console.log("_parsed: ", _parsed.value); | ||
const parsed = computed(() => edit(_parsed)); | ||
return { parsed }; | ||
} | ||
|
||
function edit(parsed: MaybeRef<string>) { | ||
const tree = $(`<div>${unref(parsed)}</div>`); | ||
|
||
// Open external links in new tab | ||
tree | ||
.find("a[href^='http']") | ||
.attr("target", "_blank") | ||
.attr("rel", "noopener noreferrer"); | ||
|
||
// Add mdi-open-in-new icon to links with target="_blank" on text (not img) | ||
tree | ||
.find("a[target='_blank']") | ||
.append( | ||
' <i class="mdi mdi-open-in-new mdi v-icon notranslate v-icon--size-xsmall" aria-hidden="true"></i>' | ||
); | ||
|
||
return tree.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