v2.0.11

Hashtag Links

Last updated


With hashtagEndpoint set, #tag in the document becomes a link to a URL you choose, such as your own tag pages; this page covers how tags are recognized and how to configure the endpoint.

Recognized Tags


SyntaxExample
# followed by letters, digits, marks or _ in any script#nanomd, #中文標籤, #123, #snake_case
Full-width ##全形
Inside other inline styles**#bold**
Inside a heading's text# heading #tag

A tag ends at the first character that is not a letter, digit, mark or _, so in #tag. the . stays outside the link.
Not a tagExample
# right after a letter, digit, _, &, # or /a#b, {, ##x
URL fragmentshttps://nanomd.pardn.io/#frag
Code spans and inline math`#code`, $#x$
Escaped\#escaped
Inside link text[label #x](https://nanomd.pardn.io)

Configuration


  1. MDParser.Init({ hashtagEndpoint: "/tags/{tag}" });

ItemRule
{tag}Replaced with encodeURIComponent(tag) without the #; may appear more than once
Unset#tag renders as plain text, and the original # or # is kept
Not a string, or missing {tag}Logs [Invalid] hashtagEndpoint: must be a string containing {tag} and is treated as unset
Output<a href="..." target="_blank" rel="noopener" class="md-link md-hashtag">#tag</a>
AST{ type: "link", tag: "hashtag", href: "tag", children: [{ type: "text", value: "#tag" }] } whether or not the endpoint is set

The endpoint is read when html() runs, so call Parse().html() again after setting hashtagEndpoint.

Examples


Without hashtagEndpoint


Tags stay plain text.
  1. <div id="out" class="nanomd"></div>
  2. <script type="text/markdown" id="md">
  3. Tags: #nanomd #中文標籤 #全形 #123
  4. </script>
  5. <script>
  6. const out = document.getElementById("out");
  7. out.innerHTML = MDParser.Parse(document.getElementById("md").textContent).html();
  8. console.log(out.querySelectorAll(".md-hashtag").length);
  9. </script>
Live preview · @pardnchiu/nanomd@2.0.11

With hashtagEndpoint


Tags become md-hashtag links; the second line shows what is left alone.
  1. <div id="out" class="nanomd"></div>
  2. <script type="text/markdown" id="md">
  3. Tags: #nanomd #中文標籤 #全形 #123, **#bold**
  4. Not tags: a#b, &#123;, https://nanomd.pardn.io/#frag, `#code`, \#escaped, [label #x](https://nanomd.pardn.io)
  5. </script>
  6. <script>
  7. const out = document.getElementById("out");
  8. MDParser.Init({ hashtagEndpoint: "https://github.com/topics/{tag}" });
  9. out.innerHTML = MDParser.Parse(document.getElementById("md").textContent).html();
  10. console.log([...out.querySelectorAll(".md-hashtag")].map((a) => a.getAttribute("href")).join("\n"));
  11. </script>
Live preview · @pardnchiu/nanomd@2.0.11
中文