add basic lowlight extension
This commit is contained in:
14
packages/extension-code-block-lowlight/README.md
Normal file
14
packages/extension-code-block-lowlight/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# @tiptap/extension-code-block-lowlight
|
||||
[](https://www.npmjs.com/package/@tiptap/extension-code-block-lowlight)
|
||||
[](https://npmcharts.com/compare/tiptap?minimal=true)
|
||||
[](https://www.npmjs.com/package/@tiptap/extension-code-block-lowlight)
|
||||
[](https://github.com/sponsors/ueberdosis)
|
||||
|
||||
## Introduction
|
||||
tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as *New York Times*, *The Guardian* or *Atlassian*.
|
||||
|
||||
## Offical Documentation
|
||||
Documentation can be found on the [tiptap website](https://tiptap.dev).
|
||||
|
||||
## License
|
||||
tiptap is open-sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap-next/blob/main/LICENSE.md).
|
||||
33
packages/extension-code-block-lowlight/package.json
Normal file
33
packages/extension-code-block-lowlight/package.json
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "@tiptap/extension-code-block-lowlight",
|
||||
"description": "code block extension for tiptap",
|
||||
"version": "2.0.0-beta.1",
|
||||
"homepage": "https://tiptap.dev",
|
||||
"keywords": [
|
||||
"tiptap",
|
||||
"tiptap extension"
|
||||
],
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/ueberdosis"
|
||||
},
|
||||
"main": "dist/tiptap-extension-code-block-lowlight.cjs.js",
|
||||
"umd": "dist/tiptap-extension-code-block-lowlight.umd.js",
|
||||
"module": "dist/tiptap-extension-code-block-lowlight.esm.js",
|
||||
"unpkg": "dist/tiptap-extension-code-block-lowlight.bundle.umd.min.js",
|
||||
"types": "dist/packages/extension-code-block-lowlight/src/index.d.ts",
|
||||
"files": [
|
||||
"src",
|
||||
"dist"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"@tiptap/core": "^2.0.0-beta.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tiptap/extension-code-block-lowlight": "^2.0.0-beta.1",
|
||||
"@types/lowlight": "^0.0.1",
|
||||
"lowlight": "^1.20.0",
|
||||
"prosemirror-inputrules": "^1.1.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import CodeBlock from '@tiptap/extension-code-block'
|
||||
import low from 'lowlight/lib/core'
|
||||
import { LowlightPlugin } from './lowlight-plugin'
|
||||
|
||||
export interface CodeBlockLowlightOptions {
|
||||
languageClassPrefix: string,
|
||||
HTMLAttributes: {
|
||||
[key: string]: any
|
||||
},
|
||||
languages: {
|
||||
[key: string]: Function
|
||||
},
|
||||
}
|
||||
|
||||
export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
|
||||
name: 'codeBlockLowlight',
|
||||
|
||||
defaultOptions: {
|
||||
languageClassPrefix: 'language-',
|
||||
HTMLAttributes: {},
|
||||
languages: {},
|
||||
},
|
||||
|
||||
onBeforeCreate() {
|
||||
Object.entries(this.options.languages).forEach(([name, mapping]) => {
|
||||
low.registerLanguage(name, mapping)
|
||||
})
|
||||
},
|
||||
|
||||
addProseMirrorPlugins() {
|
||||
return [
|
||||
LowlightPlugin({ name: 'codeBlockLowlight' }),
|
||||
]
|
||||
},
|
||||
})
|
||||
5
packages/extension-code-block-lowlight/src/index.ts
Normal file
5
packages/extension-code-block-lowlight/src/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { CodeBlockLowlight } from './code-block-lowlight'
|
||||
|
||||
export * from './code-block-lowlight'
|
||||
|
||||
export default CodeBlockLowlight
|
||||
110
packages/extension-code-block-lowlight/src/lowlight-plugin.ts
Normal file
110
packages/extension-code-block-lowlight/src/lowlight-plugin.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { Plugin, PluginKey } from 'prosemirror-state'
|
||||
import { Decoration, DecorationSet } from 'prosemirror-view'
|
||||
import { Node as ProsemirrorNode } from 'prosemirror-model'
|
||||
import low from 'lowlight/lib/core'
|
||||
|
||||
type NodeWithPos = {
|
||||
node: ProsemirrorNode,
|
||||
pos: number,
|
||||
}
|
||||
|
||||
const findBlockNodes = (doc: ProsemirrorNode) => {
|
||||
const nodes: NodeWithPos[] = []
|
||||
|
||||
doc.descendants((node, pos) => {
|
||||
if (node.isBlock) {
|
||||
nodes.push({
|
||||
node,
|
||||
pos,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return nodes
|
||||
}
|
||||
|
||||
function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) {
|
||||
const decorations: Decoration[] = []
|
||||
const blocks = findBlockNodes(doc).filter(block => block.node.type.name === name)
|
||||
|
||||
function parseNodes(nodes: any[], className: string[] = []): any {
|
||||
return nodes.map(node => {
|
||||
|
||||
const classes = [
|
||||
...className,
|
||||
...node.properties ? node.properties.className : [],
|
||||
]
|
||||
|
||||
if (node.children) {
|
||||
return parseNodes(node.children, classes)
|
||||
}
|
||||
|
||||
return {
|
||||
text: node.value,
|
||||
classes,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
blocks.forEach(block => {
|
||||
let startPos = block.pos + 1
|
||||
const nodes = low.highlightAuto(block.node.textContent).value
|
||||
|
||||
parseNodes(nodes)
|
||||
.flat()
|
||||
.map((node: any) => {
|
||||
const from = startPos
|
||||
const to = from + node.text.length
|
||||
|
||||
startPos = to
|
||||
|
||||
return {
|
||||
...node,
|
||||
from,
|
||||
to,
|
||||
}
|
||||
})
|
||||
.forEach((node: any) => {
|
||||
const decoration = Decoration.inline(node.from, node.to, {
|
||||
class: node.classes.join(' '),
|
||||
})
|
||||
decorations.push(decoration)
|
||||
})
|
||||
})
|
||||
|
||||
return DecorationSet.create(doc, decorations)
|
||||
}
|
||||
|
||||
export function LowlightPlugin({ name }: { name: string }) {
|
||||
return new Plugin({
|
||||
key: new PluginKey('highlight'),
|
||||
|
||||
state: {
|
||||
init: (_, { doc }) => getDecorations({ doc, name }),
|
||||
apply: (transaction, decorationSet, oldState, newState) => {
|
||||
// TODO: find way to cache decorations
|
||||
// https://discuss.prosemirror.net/t/how-to-update-multiple-inline-decorations-on-node-change/1493
|
||||
const oldNodeName = oldState.selection.$head.parent.type.name
|
||||
const newNodeName = newState.selection.$head.parent.type.name
|
||||
const oldNodes = findBlockNodes(oldState.doc)
|
||||
.filter(node => node.node.type.name === name)
|
||||
const newNodes = findBlockNodes(newState.doc)
|
||||
.filter(node => node.node.type.name === name)
|
||||
|
||||
// Apply decorations if selection includes named node, or transaction changes named node.
|
||||
if (transaction.docChanged && ([oldNodeName, newNodeName].includes(name)
|
||||
|| newNodes.length !== oldNodes.length)) {
|
||||
return getDecorations({ doc: transaction.doc, name })
|
||||
}
|
||||
|
||||
return decorationSet.map(transaction.mapping, transaction.doc)
|
||||
},
|
||||
},
|
||||
|
||||
props: {
|
||||
decorations(state) {
|
||||
return this.getState(state)
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user