I need to find a way to use javascript to make tags and “tag aliases” - in fact, Markdown links with Link Name containing a “#” symbol- look the same. I am using tag-aliases for tags with the same purpose I use named-links for inline references.
I manually tried to replace, in Dev Tools Inspector, the “class=page-ref” with “class=tag” and works wonders but I can’t replicate that with javascript.
For example I have this tag-alias: “[#Luni]([[Monday, 29.04.2024]] “12:00”)” - in this case I am using tag-aliases for localization purposes.
In html, the above looks like this:
<a tabindex="0" data-ref="monday, 29.04.2024" draggable="true" class="page-ref">#Luni</a>
while a simple hashtag would look like:
<a tabindex="0" data-ref="luni" draggable="true" class="tag">#Luni</a>
In rendering it looks bad if I have the two mixed:
If I change the "class=page-ref">
to class="tag">
for the “tag-alias”:
i get seamless rendering:
I have tried several ways to do this in custom.js
but neither worked. I will paste one or two such attempts below, maybe you spot what I am doing wrong (maybe the whole approach ):
document.addEventListener("DOMContentLoaded", function() {
var links = document.querySelectorAll("a.page-ref");
links.forEach(function(link) {
if (link.textContent.startsWith("[#")) {
link.classList.remove("page-ref");
link.classList.add("tag");
}
});
});
document.addEventListener("DOMContentLoaded", function() {
var links = document.querySelectorAll("a.page-ref");
links.forEach(function(link) {
var text = link.textContent.trim();
if (text.startsWith("#")) {
link.classList.remove("page-ref");
link.classList.add("tag");
}
});
});
I don’t even know if the code should target the rendered html (which is simply “#Luni”) or the text before rendering (in which case the first letters would be “[#”)
Thanks.