Skip to content

Commit

Permalink
fix: HTML block causes wrong sectioning (vfm 2.2.0) (#184)
Browse files Browse the repository at this point in the history
* fix: HTML block causes wrong sectioning (vfm 2.2.0)

- fix #183

* Update src/plugins/section.ts

Co-authored-by: akabeko <akabekobeko@gmail.com>

* fix previous commit

---------

Co-authored-by: akabeko <akabekobeko@gmail.com>
  • Loading branch information
MurakamiShinyu and akabekobeko committed Nov 5, 2023
1 parent cf4776c commit 6848823
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/plugins/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,30 @@ const sectionizeIfRequired = (node: any, ancestors: Parent[], file: VFile) => {

// check if it's HTML end tag without corresponding start tag in sibling nodes.
const isHtmlEnd = (node: any) => {
if (node.type === 'html' && node.value.startsWith('</')) {
// it's HTML end tag, check if it has corresponding start tag
const tag = /^<\/([^>\s]+)/.exec(node.value)?.[1];
const isHtmlStart = (node: any) =>
node.type === 'html' && /^<([^>\s]+)/.exec(node.value)?.[1] === tag;
const htmlStart = findAfter(parent, start, isHtmlStart);
if (node.type !== 'html') {
return false;
}

const tag = /<\/([^>\s]+)\s*>[^<]*$/.exec(node.value)?.[1];
if (!tag) {
return false;
}

// it's HTML end tag, check if it has corresponding start tag
const isHtmlStart = (node: any) =>
node.type === 'html' && new RegExp(`<${tag}\\b[^>]*>`).test(node.value);
const htmlStart = findAfter(parent, start, isHtmlStart);
if (
!htmlStart ||
parent.children.indexOf(htmlStart) > parent.children.indexOf(node)
) {
// corresponding start tag is not found in this section level,
// check if it is found earlier.
const htmlStart1 = findAfter(parent, 0, isHtmlStart);
if (
!htmlStart ||
parent.children.indexOf(htmlStart) > parent.children.indexOf(node)
htmlStart1 &&
parent.children.indexOf(htmlStart1) < parent.children.indexOf(start)
) {
// corresponding start tag not found in sibling nodes
return true;
}
}
Expand Down

0 comments on commit 6848823

Please sign in to comment.