Skip to content

Commit

Permalink
Merge pull request #62 from erickwilder/use-explicit-syntax-highlith-…
Browse files Browse the repository at this point in the history
…mappings

refactor(tiptap-extensions): Do not import the full `lowlight` library.
  • Loading branch information
philippkuehn authored Oct 13, 2018
2 parents 3c650cf + 27e473c commit b6039c7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
22 changes: 22 additions & 0 deletions examples/Components/Routes/CodeHighlighting/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,25 @@ body, .usertext {
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
}
})
]
}
}
}`;
30 changes: 23 additions & 7 deletions examples/Components/Routes/CodeHighlighting/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@
<p>
These are code blocks with <strong>automatic syntax highlighting</strong> based on highlight.js.
</p>
<pre><code v-html="javascript"></code></pre>
<pre><code v-html="css"></code></pre>
<pre><code v-html="jsExample"></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>

</editor>
</div>
</template>

<script>
import javascript from 'highlight.js/lib/languages/javascript'
import css from 'highlight.js/lib/languages/css'
import { Editor } from 'tiptap'
import {
CodeBlockHighlightNode,
Expand All @@ -29,8 +38,9 @@ import {
} from 'tiptap-extensions'
import {
javascript,
css,
javascript as jsExample,
css as cssExample,
explicitImportExample
} from './examples'
export default {
Expand All @@ -40,15 +50,21 @@ export default {
data() {
return {
extensions: [
new CodeBlockHighlightNode(),
new CodeBlockHighlightNode({
languages: {
javascript,
css
}
}),
new HardBreakNode(),
new HeadingNode({ maxLevel: 3 }),
new BoldMark(),
new CodeMark(),
new ItalicMark(),
],
javascript,
css,
jsExample,
cssExample,
explicitImportExample,
}
},
}
Expand Down
22 changes: 20 additions & 2 deletions packages/tiptap-extensions/src/nodes/CodeBlockHighlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Node, Plugin } from 'tiptap'
import { Decoration, DecorationSet } from 'prosemirror-view'
import { toggleBlockType, setBlockType, textblockTypeInputRule } from 'tiptap-commands'
import { findBlockNodes } from 'prosemirror-utils'
import low from 'lowlight'
import low from 'lowlight/lib/core'

function getDecorations(doc) {
const decorations = []
Expand All @@ -12,7 +12,7 @@ function getDecorations(doc) {

const flatten = list => list.reduce(
(a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), [],
)
)

function parseNodes(nodes, className = []) {
return nodes.map(node => {
Expand Down Expand Up @@ -63,6 +63,24 @@ function getDecorations(doc) {

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() {
return 'code_block'
}
Expand Down

0 comments on commit b6039c7

Please sign in to comment.