Skip to content

Commit

Permalink
fix: async import rehype-highlight library
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Nov 15, 2024
1 parent 004d008 commit 99b5395
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/components/NcRichText/NcRichText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,24 @@ import remarkParse from 'remark-parse'
import remarkGfm from 'remark-gfm'
import breaks from 'remark-breaks'
import remark2rehype from 'remark-rehype'
import rehypeHighlight from 'rehype-highlight'
import rehype2react from 'rehype-react'
import rehypeExternalLinks from 'rehype-external-links'
import { RouterLink } from 'vue-router'

/**
* Heavy libraries should be loaded on demand to reduce component size
*/
let rehypeHighlight // from 'rehype-highlight'
/**
* @param {Function} callback - callback function to execute after library is loaded
*/
function importRehypeLibrary(callback) {
import('rehype-highlight').then((module) => {
rehypeHighlight = module.default
callback()
})
}

export default {
name: 'NcRichText',
components: {
Expand Down Expand Up @@ -399,6 +412,7 @@ export default {
data() {
return {
parentId: GenRandomId(5),
rehypeHighlightLoaded: false,
}
},

Expand Down Expand Up @@ -459,7 +473,7 @@ export default {
},
},
})
.use(this.useExtendedMarkdown ? rehypeHighlight : undefined)
.use((this.useExtendedMarkdown && this.rehypeHighlightLoaded) ? rehypeHighlight : undefined)
// .use(rehypeAddClasses, this.markdownCssClasses)
.use(remarkPlaceholder)
.use(rehypeExternalLinks, {
Expand All @@ -476,6 +490,11 @@ export default {

if (!tag.startsWith('#')) {
if (this.useExtendedMarkdown) {
if (tag === 'code' && !this.rehypeHighlightLoaded) {
importRehypeLibrary(() => {
this.rehypeHighlightLoaded = true
})
}
let nestedNode = null
if (tag === 'li' && Array.isArray(children)
&& children[0].tag === 'input'
Expand Down

0 comments on commit 99b5395

Please sign in to comment.