mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-18 14:13:21 +08:00
Merge pull request #17 from ueberdosis/feature/add-tilde-shortcut-to-code-blocks
add tilde markdown shortcut support to code blocks
This commit is contained in:
commit
ca5a049950
@ -58,17 +58,6 @@ context('/api/extensions/code-block', () => {
|
||||
.should('not.exist')
|
||||
})
|
||||
|
||||
it('should make a code block from markdown shortcuts', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.clearContent()
|
||||
|
||||
cy.get('.ProseMirror')
|
||||
.type('``` Code')
|
||||
.find('pre>code')
|
||||
.should('contain', 'Code')
|
||||
})
|
||||
})
|
||||
|
||||
it('should parse the language from a HTML code block', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.setContent('<pre><code class="language-css">body { display: none; }</code></pre>')
|
||||
@ -79,7 +68,29 @@ context('/api/extensions/code-block', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('should make a code block for js', () => {
|
||||
it('should make a code block from backtick markdown shortcuts', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.clearContent()
|
||||
|
||||
cy.get('.ProseMirror')
|
||||
.type('``` Code')
|
||||
.find('pre>code')
|
||||
.should('contain', 'Code')
|
||||
})
|
||||
})
|
||||
|
||||
it('should make a code block from tilde markdown shortcuts', () => {
|
||||
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 with backticks', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.clearContent()
|
||||
|
||||
@ -89,4 +100,15 @@ context('/api/extensions/code-block', () => {
|
||||
.should('contain', 'Code')
|
||||
})
|
||||
})
|
||||
|
||||
it('should make a code block for js with tildes', () => {
|
||||
cy.get('.ProseMirror').then(([{ editor }]) => {
|
||||
editor.clearContent()
|
||||
|
||||
cy.get('.ProseMirror')
|
||||
.type('~~~js Code')
|
||||
.find('pre>code.language-js')
|
||||
.should('contain', 'Code')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -1,7 +1,7 @@
|
||||
# CodeBlock
|
||||
With the CodeBlock extension you can add fenced code blocks to your documents. It’ll wrap the code in `<pre>` and `<code>` HTML tags.
|
||||
|
||||
Type three backticks and a space <code>```</code> and a code block is instantly added for you.
|
||||
Type <code>``` </code> (three backticks and a space) or <code>∼∼∼ </code> (three tildes and a space) and a code block is instantly added for you. You can even specify the language, try writing <code>```css </code>. That should add a `language-css` class to the `<code>`-tag.
|
||||
|
||||
::: warning Restrictions
|
||||
The CodeBlock extension doesn’t come with styling and has no syntax highlighting built-in. It’s on our roadmap though.
|
||||
|
@ -13,7 +13,8 @@ declare module '@tiptap/core/src/Editor' {
|
||||
}
|
||||
}
|
||||
|
||||
export const inputRegex = /^```(?<language>[a-z]*)? $/
|
||||
export const backtickInputRegex = /^```(?<language>[a-z]*)? $/
|
||||
export const tildeInputRegex = /^~~~(?<language>[a-z]*)? $/
|
||||
|
||||
export default new Node<CodeBlockOptions>()
|
||||
.name('code_block')
|
||||
@ -62,6 +63,7 @@ export default new Node<CodeBlockOptions>()
|
||||
'Shift-Control-\\': () => editor.codeBlock(),
|
||||
}))
|
||||
.inputRules(({ type }) => [
|
||||
textblockTypeInputRule(inputRegex, type, ({ groups }: any) => groups),
|
||||
textblockTypeInputRule(backtickInputRegex, type, ({ groups }: any) => groups),
|
||||
textblockTypeInputRule(tildeInputRegex, type, ({ groups }: any) => groups),
|
||||
])
|
||||
.create()
|
||||
|
Loading…
Reference in New Issue
Block a user