3/10/2024 0 Comments Github markdown table of contentsTask: Place heading text as is into squared bracketsįor this step, we need to get the initial string back from the hold space.This almost finishes one part of the transformation. So, the example README.md file run against the first two lines of the sed script returns The last character is the closing round bracket. Then, the string matched by the matching group is converted to lowercase and printed \L\1 Starts with the opening round bracket and the hashtag which references the html anchor local to the page (# Then there is a matching group, which is enclosed in \(Īnd inside that matching group, it matches all characters to the end of the line. Matches one or many hashkeys at the beginning of the line followed by whitespace ^#\+\s\+ So let’s look at the next sed script line S/expression to match/replacement string/ The script makes use of the find/replace pattern of sed, which is expressed as Task: Convert uppercase letters to lowercase letters.Task: Place a hashkey in front of the heading text.The next line actually deals with three tasks at once: We start by copying the pattern space to the hold space in order to be able to modify it and the get it back unmodified again later. I will go through it line by line and try to explain what it does. The following sed logic will then transform the line as needed. We can then pipe all the heading lines into sed. The simplified example from above would actually have to look like this: Place heading text as is into squared brackets.Place the modified heading text into round brackets.Convert uppercase letters to lowercase letters.Place a hashkey in front of the heading text.Modify heading text to form the anchor text.So in order to generate the necessary link markdown text we need to do a couple of things here: From the headings my document had, I found that you need to transform the text, because the anchor github generates is a bit different from the actual heading text. Unfortunately, the example link above is a bit simplified. Subchapters are indicated by multiple hashkeys.įor a link to an anchor on the same page, we need something like In github markdown, a heading is marked with hashkeys. This should print you a nice toc which you can include in your README.md long version If you just want to copy, paste and try, here you go: The result is the most visually obnoxious (or beautiful?) command I ever had to create. But I somehow “wanted” to get it done in just one command. I could obviously have done it with a bash script, some grep pipe cut and variables and all that. I tried to quickly solve it with grep pipe sed, failed horribly, did in vim instead and hated having to do that. I could not find a way to do this with a native tag. I recently wrote a lengthy README.md file in a github repository and wanted to include a table of contents at the top.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |