From 6d29b57cc3cf69b959374e19ee1ce9d0f89d2628 Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Tue, 12 Nov 2024 17:15:48 +0100 Subject: [PATCH] feat(NcRichText): highlight code syntax if language provided Signed-off-by: Maksim Sukharev --- jest.config.js | 1 + src/components/NcRichText/NcRichText.vue | 22 ++- src/components/NcRichText/highlight.scss | 195 +++++++++++++++++++++++ 3 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 src/components/NcRichText/highlight.scss diff --git a/jest.config.js b/jest.config.js index a2c0ac9097..89904c3492 100644 --- a/jest.config.js +++ b/jest.config.js @@ -15,6 +15,7 @@ const ignorePatterns = [ 'hast-*', 'is-*', 'longest-streak', // ESM dependency of remark-gfm + 'lowlight', // ESM dependency of rehype-highlight 'markdown-table', // ESM dependency of remark-gfm 'mdast-util-*', 'micromark', diff --git a/src/components/NcRichText/NcRichText.vue b/src/components/NcRichText/NcRichText.vue index 892f14b511..004bbf2b4d 100644 --- a/src/components/NcRichText/NcRichText.vue +++ b/src/components/NcRichText/NcRichText.vue @@ -65,7 +65,7 @@ textarea { ### Flavored Markdown This component can support [Github Flavored Markdown](https://github.github.com/gfm/). -It adds such elements, as tables, task lists, strikethrough, and supports autolinks by default +It adds such elements, as tables, task lists, strikethrough, and supports code syntax highlighting and autolinks by default It is also possible to make a rendered content interactive and listen for events @@ -94,6 +94,17 @@ It is also possible to make a rendered content interactive and listen for events Table header | Column A | Column B -- | -- | -- Table row | value A | value B + +--- + +\`\`\`js +const GenRandomId = (length) => { +\treturn Math.random() +\t\t.toString(36) +\t\t.replace(/[^a-z]+/g, '') +\t\t.slice(0, length || 5) +} +\`\`\` `, } }, @@ -105,7 +116,7 @@ Table row | value A | value B return } let checkBoxIndex = 0 - lines = this.text.split('\n') + let lines = this.text.split('\n') for (let i = 0; i < lines.length; i++) { if (lines[i].includes('[ ]') || lines[i].includes('[x]')) { if (checkBoxIndex === index) { @@ -302,6 +313,7 @@ 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' @@ -447,6 +459,7 @@ export default { }, }, }) + .use(this.useExtendedMarkdown ? rehypeHighlight : undefined) // .use(rehypeAddClasses, this.markdownCssClasses) .use(remarkPlaceholder) .use(rehypeExternalLinks, { @@ -564,6 +577,11 @@ export default { }, } + +