mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 10:29:48 +02:00
fix markdown-preview example
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
|
|
||||||
import Plain from 'slate-plain-serializer'
|
import Plain from 'slate-plain-serializer'
|
||||||
import { Editor } from 'slate-react'
|
import { Editor } from 'slate-react'
|
||||||
import { Mark } from 'slate'
|
|
||||||
|
|
||||||
import Prism from 'prismjs'
|
import Prism from 'prismjs'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
@@ -16,44 +15,68 @@ Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.ins
|
|||||||
/**
|
/**
|
||||||
* Define a decorator for markdown styles.
|
* Define a decorator for markdown styles.
|
||||||
*
|
*
|
||||||
* @param {Text} text
|
|
||||||
* @param {Block} block
|
* @param {Block} block
|
||||||
|
* @return {Array}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function markdownDecorator(text, block) {
|
function markdownDecorator(block) {
|
||||||
const characters = text.characters.asMutable()
|
const string = block.text
|
||||||
const language = 'markdown'
|
const texts = block.getTexts().toArray()
|
||||||
const string = text.text
|
const grammar = Prism.languages.markdown
|
||||||
const grammar = Prism.languages[language]
|
|
||||||
const tokens = Prism.tokenize(string, grammar)
|
const tokens = Prism.tokenize(string, grammar)
|
||||||
addMarks(characters, tokens, 0)
|
const decorations = []
|
||||||
return characters.asImmutable()
|
let startText = texts.shift()
|
||||||
}
|
let endText = startText
|
||||||
|
let startOffset = 0
|
||||||
|
let endOffset = 0
|
||||||
|
let start = 0
|
||||||
|
|
||||||
function addMarks(characters, tokens, offset) {
|
function getLength(token) {
|
||||||
for (const token of tokens) {
|
|
||||||
if (typeof token == 'string') {
|
if (typeof token == 'string') {
|
||||||
offset += token.length
|
return token.length
|
||||||
continue
|
} else if (typeof token.content == 'string') {
|
||||||
|
return token.content.length
|
||||||
|
} else {
|
||||||
|
return token.content.reduce((l, t) => l + getLength(t), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
const { content, length, type } = token
|
|
||||||
const mark = Mark.create({ type })
|
|
||||||
|
|
||||||
for (let i = offset; i < offset + length; i++) {
|
|
||||||
let char = characters.get(i)
|
|
||||||
let { marks } = char
|
|
||||||
marks = marks.add(mark)
|
|
||||||
char = char.set('marks', marks)
|
|
||||||
characters.set(i, char)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(content)) {
|
|
||||||
addMarks(characters, content, offset)
|
|
||||||
}
|
|
||||||
|
|
||||||
offset += length
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const token of tokens) {
|
||||||
|
startText = endText
|
||||||
|
startOffset = endOffset
|
||||||
|
|
||||||
|
const length = getLength(token)
|
||||||
|
const end = start + length
|
||||||
|
|
||||||
|
let available = startText.text.length - startOffset
|
||||||
|
let remaining = length
|
||||||
|
|
||||||
|
endOffset = startOffset + remaining
|
||||||
|
|
||||||
|
while (available < remaining) {
|
||||||
|
endText = texts.shift()
|
||||||
|
remaining = length - available
|
||||||
|
available = endText.text.length
|
||||||
|
endOffset = remaining
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof token != 'string') {
|
||||||
|
const range = {
|
||||||
|
anchorKey: startText.key,
|
||||||
|
anchorOffset: startOffset,
|
||||||
|
focusKey: endText.key,
|
||||||
|
focusOffset: endOffset,
|
||||||
|
marks: [{ type: token.type }],
|
||||||
|
}
|
||||||
|
|
||||||
|
decorations.push(range)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
start = end
|
||||||
|
}
|
||||||
|
|
||||||
|
return decorations
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user