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

fix: HTML block causes wrong sectioning (vfm 2.2.0) #184

Merged
merged 3 commits into from
Nov 5, 2023
Merged
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
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
Loading