Skip to content

Commit

Permalink
fix syntax highlighting
Browse files Browse the repository at this point in the history
cant filter out the very format we are trying to detect
  • Loading branch information
jhchen committed Dec 27, 2018
1 parent df98522 commit 76eac21
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions blots/block.js
Expand Up @@ -165,24 +165,26 @@ class BlockEmbed extends EmbedBlot {
BlockEmbed.scope = Scope.BLOCK_BLOT;
// It is important for cursor behavior BlockEmbeds use tags that are block level elements

function blockDelta(blot) {
function blockDelta(blot, filter = true) {
return blot
.descendants(LeafBlot)
.reduce((delta, leaf) => {
if (leaf.length() === 0) {
return delta;
}
return delta.insert(leaf.value(), bubbleFormats(leaf));
return delta.insert(leaf.value(), bubbleFormats(leaf, {}, filter));
}, new Delta())
.insert('\n', bubbleFormats(blot));
}

function bubbleFormats(blot, formats = {}) {
function bubbleFormats(blot, formats = {}, filter = true) {
if (blot == null) return formats;
if (typeof blot.formats === 'function') {
formats = extend(formats, blot.formats());
// exclude syntax highlighting from deltas and getFormat()
delete formats['code-token'];
if (filter) {
// exclude syntax highlighting from deltas and getFormat()
delete formats['code-token'];
}
}
if (
blot.parent == null ||
Expand All @@ -191,7 +193,7 @@ function bubbleFormats(blot, formats = {}) {
) {
return formats;
}
return bubbleFormats(blot.parent, formats);
return bubbleFormats(blot.parent, formats, filter);
}

export { blockDelta, bubbleFormats, BlockEmbed, Block as default };
2 changes: 1 addition & 1 deletion modules/syntax.js
Expand Up @@ -112,7 +112,7 @@ class SyntaxCodeBlockContainer extends CodeBlockContainer {
if (forced || this.forceNext || this.cachedText !== text) {
if (text.trim().length > 0 || this.cachedText == null) {
const oldDelta = this.children.reduce((delta, child) => {
return delta.concat(blockDelta(child));
return delta.concat(blockDelta(child, false));
}, new Delta());
const delta = highlight(text, language);
oldDelta.diff(delta).reduce((index, { retain, attributes }) => {
Expand Down

0 comments on commit 76eac21

Please sign in to comment.