Parsing tags in markdown
<p><a class="wikilink" href="/luhmann_method/">Luhmann method</a> suggests using tags for categorizing entries. I am not sure how I feel about it, but for sure it can help re-discover new notes based on a topic, following an apparently random jumping process. Therefore, to be able to include tags into notes without a fixed structure, I wanted to parse the markdown file and identify strings that start with a <code>#</code>. The regex was inspired by the parses of <a href="https://github.com/renerocksai/sublimeless_zk/blob/master/src/libzk2setevi/zkutils.py">sublimeless</a>:</p>
<pre class="codehilite"><code class="language-python">RE_TAGS = r"(#+([^#\s.,\/!$%\^&\*;{}\[\]'\"=`~()<>”\\]|:[a-zA-Z0-9])+)"
</code></pre>
<p>The tricky part was to add it as an extension to the markdown parser in <a class="wikilink" href="/python/">python</a> so that it would not only transform the content, but it would also store it for using in the main script. I followed a similar approach to that defined for the wikilinks:</p>
<p>First, define an <code>InlineProcessor</code> that will happen after the code has been standardized. Fortunately, the extensions API is well <a href="https://python-markdown.github.io/extensions/api/">documented</a>, but I still had to go <a href="https://github.com/Python-Markdown/markdown/blob/56b03b21f50d2b28b7ab87df7d8015e1f1b62184/markdown/inlinepatterns.py#L90">to the code</a> to find out what priority to give to it. </p>
<p>In my case, when I define the <code>TagExtension</code>, I use:</p>
<pre class="codehilite"><code class="language-python">md.inlinePatterns.register(TagInlineProcessor(TagInlineProcessor.RE_TAGS, md), 'tags', 65)
</code></pre>
<p>The <strong>65</strong> means it will happen right after the <code>SimpleTextInlineProcessor</code> and the <code>AsteriskProcessor</code>, but I am not sure this is the best place.</p>
Backlinks
These are the other notes that link to this one.
Nothing links here, how did you reach this page then?
Comment
Share your thoughts on this note. Comments are not public, they are
messages sent directly to my inbox.