Skip to content

Commit 0093f2e

Browse files
committed
conditionally remove blank lines in TOC content unless they are within extended markdown tags
1 parent cb1bac9 commit 0093f2e

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

script/content-migrations/update-tocs.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const walk = require('walk-sync')
66
const frontmatter = require('../../lib/read-frontmatter')
77
const getDocumentType = require('../../lib/get-document-type')
88
const languages = require('../../lib/languages')
9+
const extendedMarkdownTags = Object.keys(require('../../lib/liquid-tags/extended-markdown').tags)
910

1011
const linkString = /{% [^}]*?link.*? (\/.*?) ?%}/m
1112
const linksArray = new RegExp(linkString.source, 'gm')
@@ -73,13 +74,19 @@ indexFiles
7374
}
7475

7576
// Remove the Table of Contents section and leave any body text before it.
76-
const newContent = content
77+
let newContent = content
7778
.replace(/^#*? Table of contents[\s\S]*/im, '')
7879
.replace('<div hidden>', '')
7980
.replace(linksArray, '')
81+
82+
const linesArray = newContent
8083
.split('\n')
81-
.filter(line => /\S/.test(line))
82-
.join('\n')
84+
85+
const newLinesArray = linesArray
86+
.filter((line, index) => /\S/.test(line) || (extendedMarkdownTags.find(tag => (linesArray[index - 1] && linesArray[index - 1].includes(tag)) || (linesArray[index + 1] && linesArray[index + 1].includes(tag)))))
87+
.filter(line => !/^<!--\s+?-->$/m.test(line))
88+
89+
newContent = newLinesArray.join('\n')
8390

8491
// Index files should no longer have body content, so we write an empty string
8592
fs.writeFileSync(indexFile, frontmatter.stringify(newContent, data, { lineWidth: 10000 }))

0 commit comments

Comments
 (0)