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 19, 2024
1 parent f8353f7 commit c523550
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/components/NcRichText/NcRichText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ See [NcRichContenteditable](#/Components/NcRichContenteditable) documentation fo
</docs>

<script>
import { ref } from 'vue'
import NcReferenceList from './NcReferenceList.vue'
import NcCheckboxRadioSwitch from '../NcCheckboxRadioSwitch/NcCheckboxRadioSwitch.vue'
import { getRoute, remarkAutolink } from './autolink.js'
Expand All @@ -313,11 +314,25 @@ 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'
const rehypeHighlightLoaded = ref(false)
/**
* @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 @@ -459,7 +474,7 @@ export default {
},
},
})
.use(this.useExtendedMarkdown ? rehypeHighlight : undefined)
.use((this.useExtendedMarkdown && rehypeHighlightLoaded.value) ? rehypeHighlight : undefined)
// .use(rehypeAddClasses, this.markdownCssClasses)
.use(remarkPlaceholder)
.use(rehypeExternalLinks, {
Expand All @@ -476,6 +491,11 @@ export default {

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

0 comments on commit c523550

Please sign in to comment.