Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to customize lineBreakSymbol #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ast-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface OptionType<T extends InputNodeTypes = InputNodeTypes> {
linkDestinationKey?: string;
imageSourceKey?: string;
imageCaptionKey?: string;
lineBreakSymbol?: string;
}

export interface MdastNode {
Expand Down
7 changes: 5 additions & 2 deletions src/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function deserialize<T extends InputNodeTypes>(
const linkDestinationKey = opts?.linkDestinationKey ?? 'link';
const imageSourceKey = opts?.imageSourceKey ?? 'link';
const imageCaptionKey = opts?.imageCaptionKey ?? 'caption';
const lineBreakSymbol = opts?.lineBreakSymbol ?? '<br>';

let children: Array<DeserializedNode<T>> = [{ text: '' }];

Expand Down Expand Up @@ -87,11 +88,13 @@ export default function deserialize<T extends InputNodeTypes>(
} as CodeBlockNode<T>;

case 'html':
if (node.value?.includes('<br>')) {
if (node.value?.includes(lineBreakSymbol)) {
const regexp = new RegExp(lineBreakSymbol, 'g');

return {
break: true,
type: types.paragraph,
children: [{ text: node.value?.replace(/<br>/g, '') || '' }],
children: [{ text: node.value?.replace(regexp, '') || '' }],
} as ParagraphNode<T>;
}
return { type: 'paragraph', children: [{ text: node.value || '' }] };
Expand Down
14 changes: 8 additions & 6 deletions src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { BlockType, defaultNodeTypes, LeafType, NodeTypes } from './ast-types';
import escapeHtml from 'escape-html';

interface Options {
nodeTypes: NodeTypes;
nodeTypes?: NodeTypes;
listDepth?: number;
ignoreParagraphNewline?: boolean;
// lineBreakSymbol is used as an attempt to persist whitespace across serialize and deserialize
lineBreakSymbol?: string;
}

const isLeafNode = (node: BlockType | LeafType): node is LeafType => {
Expand All @@ -13,16 +15,15 @@ const isLeafNode = (node: BlockType | LeafType): node is LeafType => {

const VOID_ELEMENTS: Array<keyof NodeTypes> = ['thematic_break', 'image'];

const BREAK_TAG = '<br>';

export default function serialize(
chunk: BlockType | LeafType,
opts: Options = { nodeTypes: defaultNodeTypes }
opts: Options = {}
) {
const {
nodeTypes: userNodeTypes = defaultNodeTypes,
ignoreParagraphNewline = false,
listDepth = 0,
lineBreakSymbol = '<br>',
} = opts;

let text = (chunk as LeafType).text || '';
Expand Down Expand Up @@ -73,6 +74,7 @@ export default function serialize(
{ ...c, parentType: type },
{
nodeTypes,
lineBreakSymbol,
// WOAH.
// what we're doing here is pretty tricky, it relates to the block below where
// we check for ignoreParagraphNewline and set type to paragraph.
Expand Down Expand Up @@ -107,7 +109,7 @@ export default function serialize(
chunk.parentType === nodeTypes.paragraph
) {
type = nodeTypes.paragraph;
children = BREAK_TAG;
children = lineBreakSymbol;
}

if (children === '' && !VOID_ELEMENTS.find((k) => nodeTypes[k] === type))
Expand All @@ -120,7 +122,7 @@ export default function serialize(
// we try applying formatting like to a node like this:
// "Text foo bar **baz**" resulting in "**Text foo bar **baz****"
// which is invalid markup and can mess everything up
if (children !== BREAK_TAG && isLeafNode(chunk)) {
if (children !== lineBreakSymbol && isLeafNode(chunk)) {
if (chunk.strikeThrough && chunk.bold && chunk.italic) {
children = retainWhitespaceAndFormat(children, '~~***');
} else if (chunk.bold && chunk.italic) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Deserialize and serialize mix of break tags and new lines 1`] = `
Array [
Object {
"children": Array [
Object {
"text": "a
",
},
Object {
"break": true,
"children": Array [
Object {
"text": "",
},
],
"type": "paragraph",
},
],
"type": "paragraph",
},
Object {
"children": Array [
Object {
"children": Array [
Object {
"text": "https://a.com",
},
],
"link": "https://a.com",
"type": "link",
},
Object {
"text": "
",
},
Object {
"break": true,
"children": Array [
Object {
"text": "",
},
],
"type": "paragraph",
},
],
"type": "paragraph",
},
Object {
"children": Array [
Object {
"text": "e",
},
],
"type": "paragraph",
},
]
`;

exports[`Deserialize heading with new line 1`] = `
Array [
Object {
"children": Array [
Object {
"text": "heading one",
},
],
"type": "heading_one",
},
Object {
"break": true,
"children": Array [
Object {
"text": "",
},
],
"type": "paragraph",
},
Object {
"children": Array [
Object {
"text": "Foo",
},
],
"type": "paragraph",
},
]
`;
53 changes: 53 additions & 0 deletions test/serialize/serialize-lineBreakSymbol.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import unified from 'unified';
import parse from 'remark-parse';
import plugin, { serialize } from '../../src';
import { LeafType, BlockType } from '../../src/ast-types';

const mdown = `a
<foo>

[https://a.com](https://a.com)
<foo>

e
`;

test('Deserialize and serialize mix of break tags and new lines', (done) => {
const lineBreakSymbol = '<foo>';

unified()
.use(parse)
.use(plugin, { lineBreakSymbol })
.process(mdown, (_, file) => {
const res = file.result as (LeafType | BlockType)[];
expect(res).toMatchSnapshot();

const fin = res.map((v) => serialize(v, { lineBreakSymbol })).join('');

expect(fin).toEqual(mdown);
done();
});
});

const headingWithNewLine = `# heading one
<oxo>

Foo
`;

test('Deserialize heading with new line', (done) => {
unified()
.use(parse)
.use(plugin, { lineBreakSymbol: '<oxo>' })
.process(headingWithNewLine, (_, file) => {
const res = file.result as (LeafType | BlockType)[];
expect(res).toMatchSnapshot();

const fin = res
.map((v) => serialize(v, { lineBreakSymbol: '<oxo>' }))
.join('');

expect(fin).toEqual(headingWithNewLine);
done();
});
});