From f57f51ee5220a59e9f3b306efaf6bba177dfda3c Mon Sep 17 00:00:00 2001 From: Bryce Huhtala <100382978+sketchius@users.noreply.github.com> Date: Fri, 14 Feb 2025 16:40:11 -0500 Subject: [PATCH] DEV: add support for 3, 4, and 8 digit hex codes --- .../discourse-markdown-it/src/features/hex-color.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse-markdown-it/src/features/hex-color.js b/app/assets/javascripts/discourse-markdown-it/src/features/hex-color.js index 2a09b040a04..9087714ab4f 100644 --- a/app/assets/javascripts/discourse-markdown-it/src/features/hex-color.js +++ b/app/assets/javascripts/discourse-markdown-it/src/features/hex-color.js @@ -4,9 +4,12 @@ function hexColorRule(state, silent) { const src = state.src; const firstChar = src.charCodeAt(start); + console.log("First char:", String.fromCharCode(firstChar)); + console.log("Last char:", src.charAt(max - 1)); + // early exit if first char isn't `#` // or there's less than 7 characters left - if (firstChar !== 0x23 || start + 7 > max) { + if (firstChar !== 0x23 || start + 4 > max) { return false; } @@ -15,7 +18,10 @@ function hexColorRule(state, silent) { return false; } - const match = /^#[0-9A-Fa-f]{6}(?!\w)/.exec(src.slice(start)); + const match = src + .slice(start) + .match(/^#(?:[0-9A-Fa-f]{3,4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})(?=\b|_|$)/i); + if (!match) { return false; }