I have a markdown file and I want to insert new content to it with some conditions (like sorting).
For example, if the user gives the URL & it was an Video URL (is in
pre-defined list of URLs), if the new video's upload date was older than 2022/08/06
(2022/08/04), it should create a new heading level 4 before the heading 2022/08/06
and insert the URL and body (No problem with getting details of video).
How can I do this type of manipulation in Python into the .md
file?
# Web Bookmarks
## Videos
### www.youtube.com
<INSERT_NEW_URL_HERE>
#### 2022/08/04
...
#### 2022/08/06
##### URL: 9LPB8X4KB1g
...Body...
#### 2022/08/10
##### URL: wcuG2SsDbnM
...Body...
### www.vimeo.com
...
markdown file is text file so you can use
string
functions orregex
(ie.'#### (\d{4}/\d{2}/\d{2})'
) to search#### date
and compare it with new date, and ifdate > new_date
then replacedate
withnew_date + url + body + ###
+date
(and skip other dates).Minimal working example:
I uses special construction
for/else/break
(notif/else
) to put text at the end if it couldn't finddate > new_date
(if it didn't runbreak
insidefor
-loop)It may need some changes if you have more complex data. For example it doesn't check if it has to put in
### www.youtube.com
or in### www.vimeo.com
. It would need first search part withwww.youtube.com
orwww.vimeo.com
Result: