mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-24 03:39:01 +08:00
add language support to code blocks
This commit is contained in:
parent
e14365fad6
commit
e24ce2af77
@ -59,9 +59,24 @@ context('/api/extensions/code-block', () => {
|
||||
})
|
||||
|
||||
it('should make a code block from markdown shortcuts', () => {
|
||||
cy.get('.ProseMirror')
|
||||
.type('``` {enter}Code')
|
||||
.find('pre')
|
||||
.should('contain', 'Code')
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.clearContent()
|
||||
|
||||
cy.get('.ProseMirror')
|
||||
.type('``` Code')
|
||||
.find('pre>code')
|
||||
.should('contain', 'Code')
|
||||
})
|
||||
})
|
||||
|
||||
it('should make a code block for js', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.clearContent()
|
||||
|
||||
cy.get('.ProseMirror')
|
||||
.type('```js Code')
|
||||
.find('pre>code.language-js')
|
||||
.should('contain', 'Code')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -9,9 +9,16 @@ declare module '@tiptap/core/src/Editor' {
|
||||
}
|
||||
}
|
||||
|
||||
export const inputRegex = /^```(?<language>[a-z]*)? $/
|
||||
|
||||
export default new Node()
|
||||
.name('code_block')
|
||||
.schema(() => ({
|
||||
attrs: {
|
||||
language: {
|
||||
default: null,
|
||||
},
|
||||
},
|
||||
content: 'text*',
|
||||
marks: '',
|
||||
group: 'block',
|
||||
@ -21,7 +28,7 @@ export default new Node()
|
||||
parseDOM: [
|
||||
{ tag: 'pre', preserveWhitespace: 'full' },
|
||||
],
|
||||
toDOM: () => ['pre', ['code', 0]],
|
||||
toDOM: node => ['pre', ['code', { class: node.attrs.language && `language-${node.attrs.language}` }, 0]],
|
||||
}))
|
||||
.commands(({ name }) => ({
|
||||
codeBlock: attrs => ({ commands }) => {
|
||||
@ -32,6 +39,6 @@ export default new Node()
|
||||
'Shift-Control-\\': () => editor.codeBlock(),
|
||||
}))
|
||||
.inputRules(({ type }) => [
|
||||
textblockTypeInputRule(/^```$/, type),
|
||||
textblockTypeInputRule(inputRegex, type, ({ groups }: any) => groups),
|
||||
])
|
||||
.create()
|
||||
|
Loading…
Reference in New Issue
Block a user