Skip to content

Commit

Permalink
Merge pull request #92 from inokawa/issue90
Browse files Browse the repository at this point in the history
Put whitespace outside of strong
  • Loading branch information
inokawa committed Jan 26, 2022
2 parents 481baf4 + 1e868c0 commit 34e3ea9
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 5 deletions.
126 changes: 126 additions & 0 deletions src/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10312,6 +10312,132 @@ Array [
]
`;
exports[`issues issue90 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "Italic",
},
],
"type": "strong",
},
],
"type": "emphasis",
},
Object {
"type": "text",
"value": " ",
},
Object {
"children": Array [
Object {
"type": "text",
"value": "in a bold paragraph",
},
],
"type": "strong",
},
],
"type": "paragraph",
},
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "This is an ",
},
Object {
"children": Array [
Object {
"type": "text",
"value": "Italic",
},
],
"type": "emphasis",
},
],
"type": "strong",
},
Object {
"type": "text",
"value": " ",
},
Object {
"children": Array [
Object {
"type": "text",
"value": "in a bold paragraph",
},
],
"type": "strong",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;
exports[`issues issue90 2`] = `
"***Italic*** **in a bold paragraph**
**This is an *Italic*** **in a bold paragraph**
"
`;
exports[`issues issue90 3`] = `
Array [
Object {
"children": Array [
Object {
"emphasis": true,
"strong": true,
"text": "Italic",
},
Object {
"text": " ",
},
Object {
"strong": true,
"text": "in a bold paragraph",
},
],
"type": "paragraph",
},
Object {
"children": Array [
Object {
"strong": true,
"text": "This is an ",
},
Object {
"emphasis": true,
"strong": true,
"text": "Italic",
},
Object {
"text": " ",
},
Object {
"strong": true,
"text": "in a bold paragraph",
},
],
"type": "paragraph",
},
]
`;
exports[`options override builders 1`] = `
Array [
Object {
Expand Down
53 changes: 51 additions & 2 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ describe("issues", () => {
text: " was",
},
{
text:
" four years old, and for most of his career has created two series simultaneously. ",
text: " four years old, and for most of his career has created two series simultaneously. ",
},
{
strong: true,
Expand All @@ -207,4 +206,54 @@ describe("issues", () => {
const slateTree = toSlateProcessor.processSync(text).result;
expect(slateTree).toMatchSnapshot();
});

it("issue90", () => {
const slateNodes = [
{
type: "paragraph",
children: [
{
text: "Italic",
strong: true,
emphasis: true,
},
{
strong: true,
text: " in a bold paragraph",
},
],
},
{
type: "paragraph",
children: [
{
strong: true,
text: "This is an ",
},
{
text: "Italic",
strong: true,
emphasis: true,
},
{
strong: true,
text: " in a bold paragraph",
},
],
},
];
const toSlateProcessor = unified().use(markdown).use(remarkToSlate);
const toRemarkProcessor = unified()
.use(slateToRemark)
.use(stringify, { emphasis: "*" });
const mdastTree = toRemarkProcessor.runSync({
type: "root",
children: slateNodes,
} as any);
expect(mdastTree).toMatchSnapshot();
const text = toRemarkProcessor.stringify(mdastTree);
expect(text).toMatchSnapshot();
const slateTree = toSlateProcessor.processSync(text).result;
expect(slateTree).toMatchSnapshot();
});
});
10 changes: 7 additions & 3 deletions src/transformers/slate-to-mdast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ function convertNodes(
} else {
const mdastTexts: TextOrDecoration[] = [];
const starts: DecorationType[] = [];
let ends: DecorationType[] = [];
let textTemp: string = "";
for (let j = 0; j < textQueue.length; j++) {
const cur = textQueue[j];
textTemp += cur.text;

const prevStartsStr = starts.toString();
const prevStarts = starts.slice();
const prevEnds = ends.slice();

const prev = textQueue[j - 1];
const next = textQueue[j + 1];
const ends: DecorationType[] = [];
ends = [];
(["inlineCode", "emphasis", "strong", "delete"] as const).forEach(
(k) => {
if (cur[k]) {
Expand Down Expand Up @@ -90,7 +92,9 @@ function convertNodes(
let aft = "";
if (
endsToRemove.length === 1 &&
prevStartsStr !== starts.toString() &&
(prevStarts.toString() !== starts.toString() ||
// https://github.com/inokawa/remark-slate-transformer/issues/90
(prevEnds.includes("emphasis") && ends.includes("strong"))) &&
starts.length - endsToRemove.length === 0
) {
while (textTemp.startsWith(" ")) {
Expand Down

0 comments on commit 34e3ea9

Please sign in to comment.