Skip to content

Commit

Permalink
fix: fenced code block is broken depending on input order (fix #1167) (
Browse files Browse the repository at this point in the history
…#1168)

* fix: fenced code block is broken depending on input order (fix #1167)

* fix: do not enter after closing fence code

* feat: add test case
  • Loading branch information
seonim-ryu authored Aug 28, 2020
1 parent 151f98e commit b312e15
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
7 changes: 5 additions & 2 deletions apps/editor/src/js/codemirror/continuelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CodeMirror.commands.indentOrderedList = function(cm) {
var line = cm.getLine(pos.line);
var cursorBeforeTextInline = line.substr(0, pos.ch);

if ((listRE.test(cursorBeforeTextInline) || cm.somethingSelected())) {
if (listRE.test(cursorBeforeTextInline) || cm.somethingSelected()) {
cm.indentSelection('add');
} else {
cm.execCommand('insertSoftTab');
Expand All @@ -28,7 +28,10 @@ CodeMirror.commands.indentOrderedList = function(cm) {
};

CodeMirror.commands.newlineAndIndentContinueMarkdownList = function(cm) {
if (cm.getOption('disableInput') || !!cm.state.isCursorInCodeBlock) return CodeMirror.Pass;
if (cm.getOption('disableInput') || !!cm.state.isCursorInCodeBlock) {
cm.execCommand('newlineAndIndent');
return;
}
var ranges = cm.listSelections(),
replacements = [];

Expand Down
8 changes: 8 additions & 0 deletions libs/toastmark/src/__test__/toastmark.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ describe('editText()', () => {
assertParseResult(doc, ['Hey,', 'Hello', '', 'My', '', 'World *!!*']);
assertResultNodes(doc, result.nodes);
});

it('update to fenced code blocks to the end of the document', () => {
const doc = new ToastMark('``\nHello\n\nMy World');
const result = doc.editMarkdown([1, 3], [1, 3], '`')[0];

assertParseResult(doc, ['```', 'Hello', '', 'My World']);
assertResultNodes(doc, result.nodes);
});
});

describe('list item', () => {
Expand Down
15 changes: 9 additions & 6 deletions libs/toastmark/src/toastmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
SourcePos,
isRefDef,
RefDefNode,
isTable
isTable,
isCodeBlock
} from './commonmark/node';
import {
removeNextUntil,
Expand Down Expand Up @@ -227,13 +228,15 @@ export class ToastMark {
const editedLines = this.lineTexts.slice(startLine - 1, endLine);
const root = this.parser.partialParseStart(startLine, editedLines);

// extends ending range if the following node can be a continued list item
// extends ending range if the following node can be a fenced code block or a continued list item
let nextNode = endNode ? endNode.next : this.root.firstChild;
const { lastChild } = root;
const isLastChildCodeBlock = lastChild && isCodeBlock(lastChild);
const isLastChildList = lastChild && isList(lastChild);

while (
root.lastChild &&
isList(root.lastChild) &&
nextNode &&
(nextNode.type === 'list' || nextNode.sourcepos![0][1] >= 2)
(isLastChildCodeBlock && nextNode) ||
(isLastChildList && nextNode && (nextNode.type === 'list' || nextNode.sourcepos![0][1] >= 2))
) {
const newEndLine = this.extendEndLine(nextNode.sourcepos![1][0]);
this.parser.partialParseExtends(this.lineTexts.slice(endLine, newEndLine));
Expand Down

0 comments on commit b312e15

Please sign in to comment.