How it works...
To understand the entirety of how this worked, you will need to know about regular expressions.
First, there are important considerations when using the Get-Content cmdlet. The output of the cmdlet is not a single string, but an array of strings. PowerShell terminates a string at a line break when using Get-Content. Therefore, PowerShell will be unable to match newline characters. Using the -Raw switch imports the content as a single string; we need the entire text as a single block. However, now, the ^ anchor works only for the beginning of the entire text. To compensate for this, we use ^#|\n# in the regex to match the first-level heading notation.
The -replace operator is similar to the Replace() method that's available ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access