Skip to content

Commit

Permalink
chore: edge case of formatting and whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed May 9, 2023
1 parent ea3fd1d commit 982e720
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/export/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MdxjsEsm, mdxToMarkdown } from 'mdast-util-mdx'
import { Options as ToMarkdownOptions, toMarkdown } from 'mdast-util-to-markdown'
import { LexicalExportVisitor, LexicalVisitors } from './visitors'
import { JsxComponentDescriptors } from '../types/JsxComponentDescriptors'
import { WHITESPACE_MARKER } from '../utils/whitespaceConversion'
export type { Options as ToMarkdownOptions } from 'mdast-util-to-markdown'

function isParent(node: unknown): node is Mdast.Parent {
Expand Down Expand Up @@ -159,5 +160,5 @@ export function exportMarkdownFromLexical({
extensions: [mdxToMarkdown(), frontmatterToMarkdown('yaml'), directiveToMarkdown],
listItemIndent: 'one',
...options,
})
}).replaceAll(WHITESPACE_MARKER, ' ')
}
13 changes: 9 additions & 4 deletions src/export/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from '../nodes'

import { IS_BOLD, IS_CODE, IS_ITALIC, IS_UNDERLINE } from '../FormatConstants'
import { replaceWhitespace } from '../utils/whitespaceConversion'

export type { Options as ToMarkdownOptions } from 'mdast-util-to-markdown'

Expand Down Expand Up @@ -203,11 +204,13 @@ export const LexicalTextVisitor: LexicalExportVisitor<TextNode, Mdast.Text> = {
visitLexicalNode: ({ lexicalNode, mdastParent, actions }) => {
const previousSibling = lexicalNode.getPreviousSibling()
const prevFormat = $isTextNode(previousSibling) ? previousSibling.getFormat() : 0
const format = lexicalNode.getFormat() ?? 0
let textContent = lexicalNode.getTextContent()
// if the node is only whitespace, ignore the format.
const format = /^\s+$/.test(textContent) ? 0 : lexicalNode.getFormat() ?? 0

if (format & IS_CODE) {
actions.addAndStepInto('inlineCode', {
value: lexicalNode.getTextContent(),
value: textContent,
})
return
}
Expand All @@ -226,7 +229,6 @@ export const LexicalTextVisitor: LexicalExportVisitor<TextNode, Mdast.Text> = {
children: [],
}) as Mdast.Parent
}

if (prevFormat & format & IS_UNDERLINE) {
localParentNode = actions.appendToParent(localParentNode, {
type: 'mdxJsxTextElement',
Expand Down Expand Up @@ -259,9 +261,12 @@ export const LexicalTextVisitor: LexicalExportVisitor<TextNode, Mdast.Text> = {
}) as Mdast.Parent
}

if (format !== 0) {
textContent = replaceWhitespace(textContent)
}
actions.appendToParent(localParentNode, {
type: 'text',
value: lexicalNode.getTextContent(),
value: textContent,
})
},
}
Expand Down
5 changes: 0 additions & 5 deletions src/import/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,6 @@ export function importMarkdownToLexical(
mdastExtensions: [mdxFromMarkdown(), frontmatterFromMarkdown('yaml'), directiveFromMarkdown],
})

// add a frontmatter node if there is none
if (tree.children[0].type !== 'yaml') {
tree.children.unshift({ type: 'yaml', value: '' })
}

function visitChildren(mdastNode: Mdast.Parent, lexicalParent: LexicalNode) {
if (!isParent(mdastNode)) {
throw new Error('Attempting to visit children of a non-parent')
Expand Down
2 changes: 0 additions & 2 deletions src/stories/assets/kitchen-sink-markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
title: Hello World
---

:::info
Some informative text
:::

[A link](https://google.com/ "Googl Title")

Expand Down
18 changes: 16 additions & 2 deletions src/test/impex.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ describe('converting', () => {
expect(exportMarkdownFromLexical({ root: $getRoot() })).toEqual('Hello World\n')
})
})

it.skip('moves whitespace out of formatting markers', () => {
const { editor } = testEnv
editor!.update(() => {
const paragraph = $createParagraphNode()
const node = $createTextNode('Hello World ')
node.setFormat(0b11)
paragraph.append(node)
$getRoot().append(paragraph)
expect(exportMarkdownFromLexical({ root: $getRoot() })).toEqual('**Hello World** \n')
})
})
})
})

Expand All @@ -80,7 +92,9 @@ function testIdenticalMarkdownAfterImportExport(
options: exportOptions,
visitors: undefined,
jsxComponentDescriptors,
}).trim()
})
.trim()
.replaceAll('&#x20;', ' ')
).toEqual(markdown.trim())
})
})
Expand Down Expand Up @@ -125,7 +139,7 @@ describe('markdown import export', () => {
const md = `
*Hello **world** some more*
**Hello *world* <u>some</u> more**
**Hello *world*<u>some</u> more**
`
testIdenticalMarkdownAfterImportExport(testEnv.editor!, md)
})
Expand Down
15 changes: 15 additions & 0 deletions src/utils/whitespaceConversion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const WHITESPACE_MARKER = '\u00A0'

export function replaceWhitespace(str: string) {
// Replace whitespace at the beginning of the string
while (str.charAt(0) === ' ') {
str = WHITESPACE_MARKER + str.slice(1)
}

// Replace whitespace at the end of the string
while (str.charAt(str.length - 1) === ' ') {
str = str.slice(0, -1) + WHITESPACE_MARKER
}

return str
}

0 comments on commit 982e720

Please sign in to comment.