Hello if anyone knows of a way to get python-markdown to behave in the way I’d like, or of an alternative way to do it, I’d love some help! My use case is I’m converting .md files made with Obsidian into html files. Obsidian has tags that are a pound sign followed by the tag (so like “#TagName”). When the tag is the first item on a line the pound sign is confused for a heading, even though there is no space after it.
Is there a way that I can avoid this so it only reads it as a heading if there is a space between the pound and the next word? I’m even considering some kind of find/replace logic so I can swap it out with like a link to a page that lists all the pages with that tag or something that gets run before the markdown to html conversion.
The most bulletproof way to do this seems to be to escape the
#
characters before running the document through Markdown. It might suffice to use a regex. (Insert regex and two problems joke here.) That seems promising because headings always match#\s+
whereas tags match#[^\s]
.I hope someone has an even better idea, but this ought to work.
I’m so bad at regex, would you mind explaining what’s going on with these ones?
I don’t mind at all. Beyond my explanation, you might like to try to use an online regular expression checker to explore small changes to the regex to see how it matches what it matches.
Headings always match
#\s+
, because that’s the character#
followed by whitespace (\s
) one or more times (+
). Other text matches this, but so not all matches are headings, but all headings match. (You might have# blah
in the middle of the text, which would match. If that’s a problem, then you can change the regex to^#\s+
, where^
means “from the beginning of a line”.Tags always match
#[^\s]
, which means the character#
followed by one not whitespace character. Be careful: tags match this regex, but this regex doesn’t match the entire tag. It only says “there is a tag here”.Fortunately, that doesn’t hurt, because your Python code could match
#[^\s]
and then turn that#
into\#
and thereby successfully avoid escaping the#
s at the beginning of headings. You could even use regex to do this by capturing the non-whitespace character at the beginning of the tag and “putting it back” using regex search and replace.Replace
#([^s])
with\#\1
.The parentheses capture the matching characters (the first character of the tag) and
\1
echoes back the captured characters. It would replace#a
with\#a
and so on.I hope I explained this clearly enough. I see the other folks also tried, so I hope that together, you found an explanation that works well enough for you.
Peace.
#\s+
is:#
: a literal#
\s
: any whitespace character (space, tab etc)+
: the previous thing (here the whitespace), one or more timesIn words: “a hash followed by at least one whitespace character”
#[^\s].
is:#
: a literal#
[^\s]
: a negated character class. This matches anything other than the set of characters after the^
.\s
has the same meaning as before, any whitespace character.
: matches any single characterIn words: “a hash followed by any character other than a whitespace character, then any character”.
https://regex101.com/ is really good for explaining regex