Skip to content

Commit

Permalink
traverse subtree when building lists
Browse files Browse the repository at this point in the history
improves buildLists to find all stray list items in subtree instead of
only direct descendents;
removes redundant normalize call on details
  • Loading branch information
AprilSylph committed Nov 22, 2021
1 parent c8264dc commit 2d4fc63
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/lib/npf.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const renderContent = ({ content: blocks, layout }) => {
}
});

[content, askContent, details]
[content, askContent]
.filter(variable => variable instanceof Node)
.forEach(node => {
node.normalize();
Expand Down Expand Up @@ -292,16 +292,22 @@ const splitArray = (array, index) => [
array.slice(index)
];

const buildLists = parentNode => {
const getFirstListItem = () => [...parentNode.children].find(element => element.matches('li[data-subtype$="list-item"]'));
while (getFirstListItem() !== undefined) {
const firstListItem = getFirstListItem();
const { subtype } = firstListItem.dataset;
const buildLists = rootNode => {
const getNextStrayListItem = () =>
[...rootNode.children].find(element => element.matches('li')) ||
rootNode.querySelector(':not(ul):not(ol) > li');

let listItem = getNextStrayListItem();
while (listItem !== null) {
const { parentNode } = listItem;
const { subtype } = listItem.dataset;

const listElement = document.createElement(subtype === 'ordered-list-item' ? 'ol' : 'ul');
parentNode.insertBefore(listElement, firstListItem);
parentNode.insertBefore(listElement, listItem);
while (listElement.nextElementSibling?.dataset.subtype === subtype) {
listElement.append(listElement.nextElementSibling);
}

listItem = getNextStrayListItem();
}
};

0 comments on commit 2d4fc63

Please sign in to comment.