refactor(tiptap-extensions): Do not import the full lowlight library.
BREAKING CHANGE: `CodeBlockHighlight` was importing the full `lowlight` libraries, including _all_ syntax highlightning definitions from `highlight.js`. The new behavior changes the signature of `CodeBlockHighlight` to accept an object with all syntax highlightning definitions. This means that now the user of the library __MUST__ import languages themselves and tiptap will no longer bundle the full `highlight.js` in itself.
This commit is contained in:
@@ -26,3 +26,25 @@ body, .usertext {
|
|||||||
content: attr(href)
|
content: attr(href)
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
|
|
||||||
|
export const explicitImportExample = `import javascript from 'highlight.js/lib/languages/javascript'
|
||||||
|
import { Editor } from 'tiptap'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
components: {
|
||||||
|
Editor
|
||||||
|
},
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
extensions: [
|
||||||
|
new CodeBlockHighlightNode({
|
||||||
|
languages: {
|
||||||
|
javascript,
|
||||||
|
css
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`;
|
||||||
|
|||||||
@@ -9,8 +9,14 @@
|
|||||||
<p>
|
<p>
|
||||||
These are code blocks with <strong>automatic syntax highlighting</strong> based on highlight.js.
|
These are code blocks with <strong>automatic syntax highlighting</strong> based on highlight.js.
|
||||||
</p>
|
</p>
|
||||||
<pre><code v-html="javascript"></code></pre>
|
<pre><code v-html="jsExample"></code></pre>
|
||||||
<pre><code v-html="css"></code></pre>
|
<pre><code v-html="cssExample"></code></pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Note: tiptap doesn't import syntax highlighting language definitions from highlight.js. You
|
||||||
|
<strong>must</strong> import them and initialize the extension with all languages you want to support:
|
||||||
|
</p>
|
||||||
|
<pre><code v-html="explicitImportExample"></code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</editor>
|
</editor>
|
||||||
@@ -18,6 +24,9 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import javascript from 'highlight.js/lib/languages/javascript'
|
||||||
|
import css from 'highlight.js/lib/languages/css'
|
||||||
|
|
||||||
import { Editor } from 'tiptap'
|
import { Editor } from 'tiptap'
|
||||||
import {
|
import {
|
||||||
CodeBlockHighlightNode,
|
CodeBlockHighlightNode,
|
||||||
@@ -29,8 +38,9 @@ import {
|
|||||||
} from 'tiptap-extensions'
|
} from 'tiptap-extensions'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
javascript,
|
javascript as jsExample,
|
||||||
css,
|
css as cssExample,
|
||||||
|
explicitImportExample
|
||||||
} from './examples'
|
} from './examples'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -40,15 +50,21 @@ export default {
|
|||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
extensions: [
|
extensions: [
|
||||||
new CodeBlockHighlightNode(),
|
new CodeBlockHighlightNode({
|
||||||
|
languages: {
|
||||||
|
javascript,
|
||||||
|
css
|
||||||
|
}
|
||||||
|
}),
|
||||||
new HardBreakNode(),
|
new HardBreakNode(),
|
||||||
new HeadingNode({ maxLevel: 3 }),
|
new HeadingNode({ maxLevel: 3 }),
|
||||||
new BoldMark(),
|
new BoldMark(),
|
||||||
new CodeMark(),
|
new CodeMark(),
|
||||||
new ItalicMark(),
|
new ItalicMark(),
|
||||||
],
|
],
|
||||||
javascript,
|
jsExample,
|
||||||
css,
|
cssExample,
|
||||||
|
explicitImportExample,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Node, Plugin } from 'tiptap'
|
|||||||
import { Decoration, DecorationSet } from 'prosemirror-view'
|
import { Decoration, DecorationSet } from 'prosemirror-view'
|
||||||
import { toggleBlockType, setBlockType, textblockTypeInputRule } from 'tiptap-commands'
|
import { toggleBlockType, setBlockType, textblockTypeInputRule } from 'tiptap-commands'
|
||||||
import { findBlockNodes } from 'prosemirror-utils'
|
import { findBlockNodes } from 'prosemirror-utils'
|
||||||
import low from 'lowlight'
|
import low from 'lowlight/lib/core'
|
||||||
|
|
||||||
function getDecorations(doc) {
|
function getDecorations(doc) {
|
||||||
const decorations = []
|
const decorations = []
|
||||||
@@ -63,6 +63,24 @@ function getDecorations(doc) {
|
|||||||
|
|
||||||
export default class CodeBlockHighlightNode extends Node {
|
export default class CodeBlockHighlightNode extends Node {
|
||||||
|
|
||||||
|
constructor(options = {}) {
|
||||||
|
super(options)
|
||||||
|
try {
|
||||||
|
Object.entries(this.options.languages).forEach(entry => {
|
||||||
|
const [name, mapping] = entry
|
||||||
|
low.registerLanguage(name, mapping)
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error('Invalid syntax highlight definitions: define at least one highlight.js language mapping')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get defaultOptions() {
|
||||||
|
return {
|
||||||
|
languages: {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get name() {
|
get name() {
|
||||||
return 'code_block'
|
return 'code_block'
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user