Created
November 24, 2019 04:42
-
-
Save koverholt/73fe6108230b70e355d17512666c6294 to your computer and use it in GitHub Desktop.
Remove H1 titles from beginning of Markdown files
This file contains hidden or 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
| import os | |
| buff = [] | |
| for root, dirs, files in os.walk("."): | |
| for file in files: | |
| if file.endswith(".md"): | |
| path = os.path.join(root, file) | |
| with open(path, "r") as f: | |
| print(path) | |
| for line in f: | |
| if line.strip(): | |
| buff.append(line) | |
| # Preserve newlines in file | |
| if not line.strip(): | |
| buff.append("\n") | |
| # Strip title at beginning of file | |
| if buff[0].startswith("#"): | |
| buff.pop(0) | |
| # Strip newlines at beginning of file | |
| while buff[0] == "\n": | |
| buff.pop(0) | |
| output = open(path, "w") | |
| output.write(''.join(buff)) | |
| output.close() | |
| buff = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment