-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(NcRichText): async import remark-gfm library #6259
Open
Antreesy
wants to merge
2
commits into
master
Choose a base branch
from
fix/6246/async-load-remark-gfm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+25
−6
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -311,7 +311,6 @@ import GenRandomId from '../../utils/GenRandomId.js' | |
|
||
import { unified } from 'unified' | ||
import remarkParse from 'remark-parse' | ||
import remarkGfm from 'remark-gfm' | ||
import breaks from 'remark-breaks' | ||
import remark2rehype from 'remark-rehype' | ||
import rehype2react from 'rehype-react' | ||
|
@@ -321,12 +320,22 @@ import { RouterLink } from 'vue-router' | |
/** | ||
* Heavy libraries should be loaded on demand to reduce component size | ||
*/ | ||
let remarkGfm | ||
const remarkGfmLoaded = ref(false) | ||
/** | ||
* Load 'remark-gfm' library when prop `useExtendedMarkdown` is truthy | ||
*/ | ||
async function importRemarkGfmLibrary() { | ||
const module = await import('remark-gfm') | ||
remarkGfm = module.default | ||
remarkGfmLoaded.value = true | ||
} | ||
let rehypeHighlight | ||
const rehypeHighlightLoaded = ref(false) | ||
/** | ||
* Load 'rehype-highlight' library when code block is rendered with `useExtendedMarkdown` | ||
*/ | ||
async function importRehypeLibrary() { | ||
async function importRehypeHighlightLibrary() { | ||
const module = await import('rehype-highlight') | ||
rehypeHighlight = module.default | ||
rehypeHighlightLoaded.value = true | ||
|
@@ -410,6 +419,12 @@ export default { | |
}, | ||
emits: ['interact:todo'], | ||
|
||
setup(props) { | ||
if (props.useExtendedMarkdown && !remarkGfmLoaded.value) { | ||
importRemarkGfmLibrary() | ||
} | ||
}, | ||
Comment on lines
+422
to
+426
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loads the lib only if |
||
|
||
data() { | ||
return { | ||
parentId: GenRandomId(5), | ||
|
@@ -464,7 +479,7 @@ export default { | |
useMarkdown: this.useMarkdown, | ||
useExtendedMarkdown: this.useExtendedMarkdown, | ||
}) | ||
.use(this.useExtendedMarkdown ? remarkGfm : undefined) | ||
.use((this.useExtendedMarkdown && remarkGfmLoaded.value) ? remarkGfm : undefined) | ||
.use(breaks) | ||
.use(remark2rehype, { | ||
handlers: { | ||
|
@@ -489,9 +504,11 @@ export default { | |
) | ||
|
||
if (!tag.startsWith('#')) { | ||
if (this.useExtendedMarkdown) { | ||
if (tag === 'code' && !rehypeHighlightLoaded.value) { | ||
importRehypeLibrary() | ||
if (this.useExtendedMarkdown && remarkGfmLoaded.value) { | ||
if (tag === 'code' | ||
&& attrs?.attrs?.class?.includes('language') | ||
&& !rehypeHighlightLoaded.value) { | ||
importRehypeHighlightLibrary() | ||
Comment on lines
+509
to
+511
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense 👍 |
||
} | ||
let nestedNode = null | ||
if (tag === 'li' && Array.isArray(children) | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized, we only need one variable here.