Formatting hashtags in markdown preview

Thanks for sharing this. Not sure if I should be bringing back an old topic, but where do you put the .tag css in the css file? I’m not able to get the hashtag highlights working within my Markdown documents in DEVONthink. Here are the steps I took:

  • made the above JavaScript file. I first put it in DEVONthink and copied its item link and put that in Files>Markdown>JavaScript. When I struck out in my first attempt with this, I then went back and put it as a .js file in Finder and selected that in Files>Markdown>JavaScript
  • downloaded Brett Terprstra’s “bretterpstra-2023.css” file, and put that into a folder and selected it in Files>Markdown>Style sheet
  • I put the .tag css in that bretterpstra-2023.css, at the very end of the file

But when I go into Markdown Preview mode, my hashtags within a Markdown document are not displaying any differently than other text.

Short answer: Wherever it’s appropriate

Longer answer: Assuming that there’s no other .tag rule in the CSS, anywhere. If there’s another .tag rule: after that one.

I suggest (as usual) converting your MD file to HTML inside DT. Then take a look at this HTML file’s source (either in DT or in the developer tools of your preferred browser):

  • Does the console display any errors or warnings? If so, which ones?
  • Is the JavaScript really included in the HTML?
  • Is the CSS really included in the HTML?
  • Look for one of your hashtags in the source: What does it look like, i.e. is it part of a span element with the correct class?

Thank you for the reply. I really appreciate you taking the time to walk through things. I suspect this is going to be beyond my ability.

  • I poked around in the console, unsure where I’m looking, but found one error for DEVONthink, but from several hours ago when I wasn’t even at the machine or working on the CSS
  • I’m not sure how to determine if the JavaScript or CSS is really included in the HTML, but I suspect not. I converted to HTML, and used “view source” in my browser. There are no elements around the hashtags I used in my document.

So, I clearly screwed this up somehow, but I’m not sure how. There are no other instances of .tag in my css file, so I added it to the end. Screenshot of only the last several lines:

Here’s my JavaScript file, in DEVONthink:

That gives me a link of x-devonthink-item://D9D3556B-C60E-41C1-932D-23A87C827E66, which is what I put in the JavaScript field in Files>Markdown>JavaScript:

CleanShot 2023-12-13 at 11.01.41

I understand it is next to impossible to diagnose these things remotely. This is a minor deal for me, so don’t waste your time trying to figure this out.

The error is in the regular expression in the JavaScript code. Sorry for not having spotted that earlier.

The RE in the script is
(#[a-zA-Z0-9_]\/+)
which means:

  • match a ‘#’ sign followed by
  • exactly one of the ASCII letters (lower or upper case), a number or an underscore
  • followed by at least one slash

The bold part is bloody nonsense (I know, it’s not your code. It is still nonsense) unless you want to highlight only those tags that terminate with at least one slash (like #tag/)
Change the Regular Expression to
(#[a-zA-Z0-9_]+)

That at least finds simple things like #tag, #underlined_tag, and #longHashWithANumber12_3 (it won’t find anything with a slash in it, though).

Now, where to put the JavaScript… As long as you’re only looking at rendered (previewed) Markdown in DT, putting the file in DT and referencing it with an item link should be ok.

However, if you’re going to convert the MD into HTML, you’re better off putting the JavaScript file somewhere outside DT (Documents, Desktop– wherever) and referencing that location in DT’s preferences. That is because the browser can’t resolve item links, so it can’t find the JavaScript inside DT.

1 Like

Thanks for sticking with me on this. That did the trick. Works perfectly! Much appreciated.

You’re welcome

I’ve noticed one minor issue, I presume with the regular expression. If I have a header tag, followed by text in wikilink brackets, the line with the text in the brackets doesn’t render. I tried this with a couple different style sheets, to at least somewhat rule out a style sheet problem.

Original text in Source mode:

How it renders in Preview mode, using the style sheet from this thread:

CleanShot 2023-12-14 at 15.42.21@2x

How it renders in Preview mode, using Brett Terpstra’s “TidBITS” StyleSheet:

That’s a rendering issue, nothing to do with CSS. I doubt that wikilinks are possible in headings

OK, thanks. I always use the header to put the topic that follows, and the topic is what I link out of.