Render preview of a color in a file

Hi Alan,

Thank you for your creative solution. It helps me greatly!

I made a small modification to the regular expression find string to include the following items:

  • find hex colour strings with capital letters
  • skip the converted strings (i.e., the script can be run multiple times to convert the new hex strings without affecting the ones already converted)

Hope this will be handy for others too.

<%* 
// Get the text contents of the current file
const file = app.workspace.getActiveFile()
let contents = await app.vault.read(file)
// Replace HTML colour codes
contents = contents.replace(/#([a-fA-F0-9]{6}(?!">■<))/g, '<span style="color: #$1">■</span><kbd><span>#</span>$1</kbd>')
// Save the new contents back to the current file
await app.vault.modify(file, contents)
-%>
3 Likes