Skip to content

Commit

Permalink
✂️ Extract parts is more careful to remove parts from tree (#1055)
Browse files Browse the repository at this point in the history
* ✂️ Extract parts is more careful to remove parts from tree
* 🧱 Handle blocks with empty children
  • Loading branch information
fwkoch committed Apr 4, 2024
1 parent 5b6f367 commit 5aa3ea2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/flat-hounds-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'myst-common': patch
'myst-parser': patch
---

Handle blocks with empty children
5 changes: 5 additions & 0 deletions .changeset/funny-walls-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-common': patch
---

Extract parts is more careful to remove parts from tree
14 changes: 11 additions & 3 deletions packages/myst-common/src/extractParts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ function createPartBlock(
return block;
}

function forcedRemove(tree: GenericParent, test: string) {
let success = remove(tree, test);
if (!success) {
success = remove(tree, { cascade: false }, test);
}
return success;
}

/**
* Extract implicit part based on heading name
*
Expand All @@ -68,7 +76,7 @@ export function extractImplicitPart(
let insideImplicitPart = false;
const blockParts: GenericNode[] = [];
let paragraphs: GenericNode[] = [];
tree.children.forEach((child, index) => {
tree.children?.forEach((child, index) => {
// Add this paragraph to the part
if (insideImplicitPart && child.type === 'paragraph') {
paragraphs.push(copyNode(child));
Expand Down Expand Up @@ -104,7 +112,7 @@ export function extractImplicitPart(
});
if (blockParts.length === 0) return;
const partsTree = { type: 'root', children: blockParts } as GenericParent;
remove(tree, '__part_delete__');
forcedRemove(tree, '__part_delete__');
return partsTree;
}

Expand Down Expand Up @@ -156,6 +164,6 @@ export function extractPart(
blockParts.forEach((block) => {
(block as any).type = '__delete__';
});
remove(tree, '__delete__');
forcedRemove(tree, '__delete__');
return partsTree;
}
3 changes: 0 additions & 3 deletions packages/myst-parser/src/tokensToMyst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,6 @@ export function tokensToMyst(
let lastBlock: GenericNode | undefined;
const pushBlock = () => {
if (!lastBlock) return;
if (lastBlock.children?.length === 0) {
delete lastBlock.children;
}
newTree.children.push(lastBlock);
};
(tree as GenericNode).children?.forEach((node) => {
Expand Down

0 comments on commit 5aa3ea2

Please sign in to comment.