Skip to content

Commit

Permalink
Fix marks with leading and trailing spaces (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored Dec 11, 2023
1 parent d3a0cd9 commit 904158b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-onions-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Fix marks with leading and trailing spaces
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,34 @@ test('code and bold', () => {
"
`);
});

test('italic in bold', () => {
const markdoc = `**a *b* c**`;
const editor = fromMarkdoc(markdoc);
expect(editor).toMatchInlineSnapshot(`
<editor>
<paragraph>
<text
bold={true}
>
a
</text>
<text
bold={true}
italic={true}
>
b
</text>
<text
bold={true}
>
c
</text>
</paragraph>
</editor>
`);
expect(toMarkdoc(editor)).toMatchInlineSnapshot(`
"**a** ***b*** **c**
"
`);
});
21 changes: 16 additions & 5 deletions packages/keystatic/src/form/fields/document/markdoc/to-markdoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,27 @@ function toMarkdocInline(node: Descendant): Node | Node[] {
mark => mark !== 'text' && mark !== 'code'
) as Mark[]
).sort();
let markdocNode = node.code
? new Ast.Node('code', { content: node.text }, [])
: new Ast.Node('text', { content: node.text });
const leadingWhitespace = /^\s+/.exec(node.text)?.[0];
const trailingWhitespace = /\s+$/.exec(node.text)?.[0];

let children = node.code
? [new Ast.Node('code', { content: node.text.trim() }, [])]
: [new Ast.Node('text', { content: node.text.trim() })];
for (const mark of marks) {
const config = markToMarkdoc[mark];
if (config) {
markdocNode = new Ast.Node(config.type, {}, [markdocNode], config.tag);
children = [new Ast.Node(config.type, {}, children, config.tag)];
}
}
return markdocNode;

if (leadingWhitespace?.length) {
children.unshift(new Ast.Node('text', { content: leadingWhitespace }, []));
}
if (trailingWhitespace?.length) {
children.push(new Ast.Node('text', { content: trailingWhitespace }, []));
}

return children;
}

export function toMarkdocDocument(
Expand Down

2 comments on commit 904158b

@vercel
Copy link

@vercel vercel bot commented on 904158b Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 904158b Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

keystatic – ./dev-projects/next-app

keystatic-git-main-thinkmill-labs.vercel.app
keystatic.vercel.app
keystatic-thinkmill-labs.vercel.app

Please sign in to comment.