Created
October 9, 2023 17:48
-
-
Save scvnc/597a10b6ecf31927aa3e343292a83a94 to your computer and use it in GitHub Desktop.
Hacktacular script that quashes markdown headers to their highest size, with header-2 being the largest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
// Check if a Markdown file is provided as a command-line argument | |
const filePath = process.argv[2]; | |
if (!filePath) { | |
console.error('Please provide a Markdown file as a command-line argument.'); | |
process.exit(1); | |
} | |
// Read the content of the Markdown file | |
let markdownContent = fs.readFileSync(filePath, 'utf8'); | |
markdownContent = markdownContent.replace(/\r/g, ""); | |
// Regular expression to match Markdown headings and capture their level and text | |
const headingRegex = /^(#+)\s+(.*)$/gm; | |
const headings = []; | |
let match; | |
while ((match = headingRegex.exec(markdownContent)) !== null) { | |
const level = match[1].length; | |
const text = match[2]; | |
headings.push({ level, text }); | |
} | |
let prevLevel = 1; | |
let first = true; | |
for (let [idx, obj] of headings.entries()) { | |
if(idx === 0) { | |
obj.newLevel = 1 | |
} else { | |
obj.newLevel = 2; | |
} | |
const mdHeading = (numHash) => new Array(numHash).fill('#').join('') | |
// const reg = new RegExp( | |
// `^\\s*${mdHeading(obj.level)}\\s+${obj.text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*$`, 'gm') | |
const reg = `${mdHeading(obj.level)} ${obj.text}` | |
markdownContent = markdownContent.replace(reg, `${mdHeading(obj.newLevel)} ${obj.text}`); | |
} | |
fs.writeFileSync(filePath, markdownContent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment