remove languages option from codeblocklowlight

This commit is contained in:
Philipp Kühn
2021-04-08 22:13:50 +02:00
parent 95f31f48fd
commit bd392ee8c2
4 changed files with 35 additions and 64 deletions

View File

@@ -1,12 +1,15 @@
<template>
<node-view-wrapper class="code-block">
<select contenteditable="false" class="code-block-select" v-model="selectedLanguage">
<option v-for="(language, index) in languages" :value="language" :key="index">
{{ language }}
</option>
<select contenteditable="false" v-model="selectedLanguage">
<option :value="null">
auto
</option>
<option disabled>
</option>
<option v-for="(language, index) in languages" :value="language" :key="index">
{{ language }}
</option>
</select>
<pre><node-view-content as="code" /></pre>
</node-view-wrapper>
@@ -14,6 +17,7 @@
<script>
import { NodeViewWrapper, NodeViewContent, nodeViewProps } from '@tiptap/vue-2'
import lowlight from 'lowlight/lib/core'
export default {
components: {
@@ -23,6 +27,12 @@ export default {
props: nodeViewProps,
data() {
return {
languages: lowlight.listLanguages(),
}
},
computed: {
selectedLanguage: {
get() {
@@ -30,26 +40,20 @@ export default {
},
set(language) {
this.updateAttributes({ language })
}
}
},
data() {
return {
languages: ['js', 'css'],
}
},
},
}
</script>
<style>
<style lang="scss" scoped>
.code-block {
position: relative;
}
.code-block-select {
select {
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
}
</style>

View File

@@ -14,11 +14,10 @@ import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
import CodeBlockComponent from './CodeBlockComponent'
import javascript from 'highlight.js/lib/languages/javascript'
import css from 'highlight.js/lib/languages/css'
import CodeBlock from './CodeBlock'
// install all highlight.js languages
import 'lowlight'
export default {
components: {
@@ -37,16 +36,9 @@ export default {
Document,
Paragraph,
Text,
CodeBlockLowlight
.extend({
CodeBlockLowlight.extend({
addNodeView() {
return VueNodeViewRenderer(CodeBlock)
},
})
.configure({
languages: {
javascript,
css,
return VueNodeViewRenderer(CodeBlockComponent)
},
}),
],

View File

@@ -15,8 +15,8 @@ import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
import javascript from 'highlight.js/lib/languages/javascript'
import css from 'highlight.js/lib/languages/css'
// install all highlight.js languages
import 'lowlight'
export default {
components: {
@@ -35,12 +35,7 @@ export default {
Document,
Paragraph,
Text,
CodeBlockLowlight.configure({
languages: {
javascript,
css,
},
}),
CodeBlockLowlight,
],
content: `
<p>

View File

@@ -1,27 +1,7 @@
import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'
import lowlight from 'lowlight/lib/core'
import CodeBlock from '@tiptap/extension-code-block'
import { LowlightPlugin } from './lowlight-plugin'
export interface CodeBlockLowlightOptions extends CodeBlockOptions {
languages: {
[key: string]: Function,
},
}
export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
defaultOptions: {
...CodeBlock.config.defaultOptions,
languages: {},
},
onBeforeCreate() {
Object
.entries(this.options.languages)
.forEach(([name, mapping]) => {
lowlight.registerLanguage(name, mapping)
})
},
export const CodeBlockLowlight = CodeBlock.extend({
addProseMirrorPlugins() {
return [
LowlightPlugin({ name: 'codeBlock' }),