From b3bc8b10e46b379c3b3e053cd04d5ddc2c97c8e3 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 15 May 2026 15:27:49 +0900 Subject: [PATCH] feat(highlight): add MdRenderInlineCode bg so code stays distinct from headings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backtick-wrapped inline code was hardcoded to the `String` highlight group. In many colorschemes `String` and the lower-level heading groups (H3+) end up with the same green-ish foreground, making `code` text indistinguishable from headings in the rendered buffer. Introduce a dedicated `MdRenderInlineCode` group: - fg: inherits from `String` so the existing color cue is preserved - bg: subtle neutral tint (Comment fg blended with Normal/NormalFloat bg at 0.18) — same pattern as `MdRenderImagePlaceholder`, mirroring how source-buffer treesitter highlights inline code Both backtick spans (`restore_code_spans`) and HTML `` tags (`HTML_TAG_HIGHLIGHTS`) route through the new group. Co-Authored-By: Claude Opus 4.7 (1M context) --- doc/md-render.jax | 4 ++++ doc/md-render.txt | 4 ++++ lua/md-render/init.lua | 22 ++++++++++++++++++++++ lua/md-render/markdown.lua | 6 +++--- tests/markdown_checkbox_test.lua | 4 ++-- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/doc/md-render.jax b/doc/md-render.jax index 1283daa..3336ff7 100644 --- a/doc/md-render.jax +++ b/doc/md-render.jax @@ -812,6 +812,10 @@ Markdown をレンダリングしてバッファに表示する完全な例で *Bold* 太字(フォールバック; `bold = true` ) *Italic* 斜体(フォールバック; `italic = true` ) + *MdRenderInlineCode* インラインコード( `` `code` `` および `` )。 + fg は `String` を継承し、bg は控えめなニュートラル色 + (Comment fg と Normal bg のブレンド)でコードと + 見出しを視覚的に区別する。 *MdRenderHighlight* Obsidian `==highlight==` マーカー *MdRenderTag* Obsidian `#tag` ( `Label` にリンク) *MdRenderMath* 数式( `Special` にリンク) diff --git a/doc/md-render.txt b/doc/md-render.txt index c3cdf31..446d833 100644 --- a/doc/md-render.txt +++ b/doc/md-render.txt @@ -802,6 +802,10 @@ INLINE FORMATTING ~ *Bold* Bold text (fallback; `bold = true`) *Italic* Italic text (fallback; `italic = true`) + *MdRenderInlineCode* Inline code spans (`` `code` `` and ``). + fg follows `String`; bg is a subtle neutral tint + (Comment fg blended with Normal bg) so code is + visibly distinct from headings. *MdRenderHighlight* Obsidian `==highlight==` markers *MdRenderTag* Obsidian `#tag` syntax (links to `Label`) *MdRenderMath* Inline/block math (links to `Special`) diff --git a/lua/md-render/init.lua b/lua/md-render/init.lua index b791c7f..7d3985a 100644 --- a/lua/md-render/init.lua +++ b/lua/md-render/init.lua @@ -97,6 +97,7 @@ function M.setup_highlights() M.setup_alert_highlights() M.setup_details_highlights() M.setup_image_placeholder_highlight() + M.setup_inline_code_highlight() end --- Set up image placeholder highlight group (MdRenderImagePlaceholder) @@ -112,6 +113,27 @@ function M.setup_image_placeholder_highlight() vim.api.nvim_set_hl(0, "MdRenderImagePlaceholder", { fg = fg, bg = bg, default = true }) end +--- Set up inline code highlight group (MdRenderInlineCode). +--- fg follows String so the existing color cue is preserved; bg is a subtle +--- neutral tint (Comment fg blended with Normal bg) so backtick spans stay +--- visually distinct from headings even when the colorscheme paints both green. +function M.setup_inline_code_highlight() + local normal_hl = vim.api.nvim_get_hl(0, { name = "NormalFloat", link = false }) + if not normal_hl.bg then + normal_hl = vim.api.nvim_get_hl(0, { name = "Normal", link = false }) + end + local normal_bg = normal_hl.bg or 0x1e1e2e + local comment_hl = vim.api.nvim_get_hl(0, { name = "Comment", link = false }) + local tint = comment_hl.fg or 0x888888 + local bg = blend_color(tint, normal_bg, 0.18) + local string_hl = vim.api.nvim_get_hl(0, { name = "String", link = false }) + if string_hl.fg then + vim.api.nvim_set_hl(0, "MdRenderInlineCode", { fg = string_hl.fg, bg = bg, default = true }) + else + vim.api.nvim_set_hl(0, "MdRenderInlineCode", { bg = bg, default = true }) + end +end + -- Re-export submodules (preview is lazy-loaded to avoid circular dependency) M.ContentBuilder = require("md-render.content_builder").ContentBuilder M.Markdown = require "md-render.markdown" diff --git a/lua/md-render/markdown.lua b/lua/md-render/markdown.lua index b938b8c..6ab1c61 100644 --- a/lua/md-render/markdown.lua +++ b/lua/md-render/markdown.lua @@ -1057,7 +1057,7 @@ local HTML_TAG_HIGHLIGHTS = { strong = "Bold", i = "Italic", em = "Italic", - code = "String", + code = "MdRenderInlineCode", s = "DiagnosticDeprecated", del = "DiagnosticDeprecated", strike = "DiagnosticDeprecated", @@ -1562,8 +1562,8 @@ Markdown.render = function(text, repo_base_url, autolinks, ref_links, footnote_m rendered_text = process_paired_markers(rendered_text, "==([^=]+)==", "MdRenderHighlight", 2, highlights, links) rendered_text = process_inline_math(rendered_text, "MdRenderMath", highlights, links) - -- Restore code spans (adds String highlight for each span) - rendered_text = restore_code_spans(rendered_text, code_spans, "String", highlights, links) + -- Restore code spans (adds MdRenderInlineCode highlight for each span) + rendered_text = restore_code_spans(rendered_text, code_spans, "MdRenderInlineCode", highlights, links) -- Restore backslash-escaped characters (adjusts highlight/link positions) rendered_text = restore_backslashes(rendered_text, backslash_escapes, highlights, links) diff --git a/tests/markdown_checkbox_test.lua b/tests/markdown_checkbox_test.lua index 90e5462..6e7befd 100644 --- a/tests/markdown_checkbox_test.lua +++ b/tests/markdown_checkbox_test.lua @@ -451,10 +451,10 @@ test("html: renders as Italic", function() assert_eq(highlights[1].hl, "Italic", "html em: should be Italic") end) -test("html: renders as String", function() +test("html: renders as MdRenderInlineCode", function() local text, highlights = render("code") assert_eq(text, "code", "html code: tags should be stripped") - assert_eq(highlights[1].hl, "String", "html code: should be String") + assert_eq(highlights[1].hl, "MdRenderInlineCode", "html code: should be MdRenderInlineCode") end) test("html: renders as strikethrough", function()