Repository: nhn/tui.editor
Pull request #2102 (2021-12-09 12:13:57)

fix: cannot execute markdown `list` command below the heading text
<!-- EDIT TITLE PLEASE -->
<!-- It should be one of them
  <ISSUE TYPE>: Short Description (<CLOSING TYPE> #<ISSUE NUMBERS>)
  ex)
  feat: add new feature (close #111)
  fix: wrong behavior (fix #111)
  chore: change build tool (ref #111)
-->

<!-- SPECIFY A ISSUE TYPE AT HEAD
  feat: A new feature
  fix: A bug fix
  docs: Documentation only changes
  style: Changes that do not affect the meaning of the code (white-space, formatting etc)
  refactor: A code change that neither fixes a bug or adds a feature
  perf: A code change that improves performance
  test: Adding missing tests
  chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
-->

<!-- ADD CLOSING TYPE AND ISSUE NUMBER AT TAIL
  (<CLOSING TYPE> #<ISSUE NUMBERS>)
  close: resolve not a bug(feature, docs, etc) completely
  fix: resolve a bug completely
  ref: not fully resolved or related to
-->

### Please check if the PR fulfills these requirements
- [x] It's the right issue type on the title
- [ ] When resolving a specific issue, it's referenced in the PR's title (e.g. `fix #xxx[,#xxx]`, where "xxx" is the issue number)
- [x] The commit message follows our guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [ ] Docs have been added/updated (for bug fixes/features)
- [ ] It does not introduce a breaking change or has a description of the breaking change

### Description
* related issue(#1859)


---
Thank you for your contribution to TOAST UI product. 🎉 😘 ✨



Patch:
diff --git a/apps/editor/src/markdown/marks/listItem.ts b/apps/editor/src/markdown/marks/listItem.ts
index f63b28c7..dd98f492 100644
--- a/apps/editor/src/markdown/marks/listItem.ts
+++ b/apps/editor/src/markdown/marks/listItem.ts
@@ -24,8 +24,11 @@ import { getTextContent } from '../helper/query';
 
 type CommandType = 'bullet' | 'ordered' | 'task';
 
-function cannotBeListNode({ type }: MdNode) {
-  return type === 'codeBlock' || type === 'heading' || type.indexOf('table') !== -1;
+function cannotBeListNode({ type, sourcepos }: MdNode, line: number) {
+  // eslint-disable-next-line prefer-destructuring
+  const startLine = sourcepos![0][0];
+
+  return line <= startLine && (type === 'codeBlock' || type === 'heading' || type.match('table'));
 }
 
 interface RangeInfo {
@@ -135,7 +138,7 @@ export class ListItem extends Mark {
       for (let line = startLine; line <= endLine; line += 1) {
         const mdNode: MdNode = toastMark.findFirstNodeAtLine(line)!;
 
-        if (mdNode && cannotBeListNode(mdNode)) {
+        if (mdNode && cannotBeListNode(mdNode, line)) {
           break;
         }
 


Test patch:
diff --git a/apps/editor/src/__test__/unit/markdown/mdCommand.spec.ts b/apps/editor/src/__test__/unit/markdown/mdCommand.spec.ts
index aeb81cef..5b9b268f 100644
--- a/apps/editor/src/__test__/unit/markdown/mdCommand.spec.ts
+++ b/apps/editor/src/__test__/unit/markdown/mdCommand.spec.ts
@@ -535,6 +535,15 @@ describe('bulletList command', () => {
 
     expect(getPreviewHTML()).toBe(result);
   });
+
+  it('should add bullet list syntax to empty line after heading node', () => {
+    mde.setMarkdown('# heading\n');
+
+    mde.setSelection([2, 1], [2, 1]);
+    cmd.exec('bulletList');
+
+    expect(getTextContent(mde)).toBe('# heading\n* ');
+  });
 });
 
 describe('orderedList command', () => {
@@ -732,6 +741,15 @@ describe('orderedList command', () => {
 
     expect(getPreviewHTML()).toBe(result);
   });
+
+  it('should add ordered list syntax to empty line after heading node', () => {
+    mde.setMarkdown('# heading\n');
+
+    mde.setSelection([2, 1], [2, 1]);
+    cmd.exec('orderedList');
+
+    expect(getTextContent(mde)).toBe('# heading\n1. ');
+  });
 });
 
 describe('taskList command', () => {