Skip to content

Commit

Permalink
Allow to insert adjacent ordered lists
Browse files Browse the repository at this point in the history
  • Loading branch information
antoine committed Dec 3, 2023
1 parent 70c38bf commit 301a620
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions __tests__/serialize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ describe('serialize', () => {
expect(serialize('<ol start="10"><li>example1</li><li>example2</li></ol>'))
.toEqual('10. example1\n11. example2');
});
test('adjacent ordered list', () => {
expect(serialize('<ol><li>example1</li></ol><ol><li>example2</li></ol><ol><li>example3</li></ol>'))
.toEqual('1. example1\n\n\n1) example2\n\n\n1. example3'); // prosemirror-markdown insert 3 \n, only 2 are needed
})
test('fence', () => {
expect(serialize('<pre><code class="language-js">example</code></pre>')).toEqual('```js\nexample\n```');
})
Expand Down
17 changes: 14 additions & 3 deletions src/extensions/nodes/ordered-list.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import { Node } from "@tiptap/core";
import { defaultMarkdownSerializer } from "prosemirror-markdown";


const OrderedList = Node.create({
name: 'orderedList',
});

function findIndexOfAdjacentNode(node, parent, index) {
let i = 0;
for (; index - i > 0; i++) {
if (parent.child(index - i - 1).type.name !== node.type.name) {
break;
}
}
return i;
}

export default OrderedList.extend({
/**
* @return {{markdown: MarkdownNodeSpec}}
*/
addStorage() {
return {
markdown: {
serialize(state,node) {
serialize(state, node, parent, index) {
const start = node.attrs.start || 1
const maxW = String(start + node.childCount - 1).length
const space = state.repeat(" ", maxW + 2)
const adjacentIndex = findIndexOfAdjacentNode(node, parent, index);
const separator = adjacentIndex % 2 ? ') ' : '. ';
state.renderList(node, space, i => {
const nStr = String(start + i)
return state.repeat(" ", maxW - nStr.length) + nStr + ". "
return state.repeat(" ", maxW - nStr.length) + nStr + separator;
})
},
parse: {
Expand Down

0 comments on commit 301a620

Please sign in to comment.