Skip to content

Commit 1238e9b

Browse files
committed
feat(mdviewer): highlight specific table cell based on CM column
Count pipe characters before CM cursor position to determine the table column index. Send tableCol with MDVIEWR_SCROLL_TO_LINE so the viewer highlights the exact cell in the row, not just the first cell with the matching source line.
1 parent 005e8a2 commit 1238e9b

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src-mdviewer/src/bridge.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,13 +932,12 @@ function _getSourceLineFromElement(el) {
932932
}
933933

934934
function handleScrollToLine(data) {
935-
const { line, fromScroll } = data;
935+
const { line, fromScroll, tableCol } = data;
936936
if (line == null) return;
937937

938938
const viewer = document.getElementById("viewer-content");
939939
if (!viewer) return;
940940

941-
942941
const elements = viewer.querySelectorAll("[data-source-line]");
943942
let bestEl = null;
944943
let bestLine = -1;
@@ -952,6 +951,15 @@ function handleScrollToLine(data) {
952951

953952
if (!bestEl) return;
954953

954+
// For table cells: if tableCol is specified, find the specific cell in the row
955+
if (tableCol != null && bestEl.closest("tr")) {
956+
const tr = bestEl.closest("tr");
957+
const cells = tr.querySelectorAll("td, th");
958+
if (tableCol < cells.length) {
959+
bestEl = cells[tableCol];
960+
}
961+
}
962+
955963
const container = document.getElementById("app-viewer");
956964
if (!container) return;
957965
const containerRect = container.getBoundingClientRect();

src/extensionsIntegrated/Phoenix-live-preview/MarkdownSync.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,21 @@ define(function (require, exports, module) {
567567
return;
568568
}
569569
// CM5 cursor line is 0-based; source lines in markdown are 1-based
570-
const line = cm.getCursor().line + 1;
570+
const cursor = cm.getCursor();
571+
const line = cursor.line + 1;
572+
// For table rows, determine column by counting | before cursor
573+
const lineText = cm.getLine(cursor.line) || "";
574+
let tableCol = null;
575+
if (lineText.trim().startsWith("|")) {
576+
const beforeCursor = lineText.substring(0, cursor.ch);
577+
// Count pipe characters (column separators) — first | is before col 0
578+
const pipes = (beforeCursor.match(/\|/g) || []).length;
579+
tableCol = Math.max(0, pipes - 1);
580+
}
571581
iframeWindow.postMessage({
572582
type: "MDVIEWR_SCROLL_TO_LINE",
573-
line: line
583+
line: line,
584+
tableCol: tableCol
574585
}, "*");
575586
}
576587

0 commit comments

Comments
 (0)