Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [14.2.1] - Unreleased
### Fixed
- Positions of table cells (`th_open`/`td_open`, `inline`, `th_close`/`td_close`)
in rows without a leading or trailing `|`. Without a leading `|`, every
cell was one character too far right (`a | b` put the second `th_open` on
the space after `|`); the first open token now sits at the start of its
cell with `size` 0. Without a trailing `|`, the last close token had
`size` 1 and covered the newline; it now sits at the end of the cell with
`size` 0. Autocompleted cells (missing from a row) sit at the end of the
line with `size` 0 instead of past it. Unicode whitespace before a row
(e.g. U+00A0, removed by `trim()` but not counted by `tShift`) no longer
moves its cells to the left. Header `inline` tokens have
`map` set to the header line instead of the delimiter line. Rendered HTML
is unchanged.


## [14.2.0] - 2026-11-18
### Changed
- `meta.srcLineStarts` of `fence` and `code_block` tokens is the source offset
Expand Down
100 changes: 68 additions & 32 deletions lib/rules_block/table.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,56 @@ function getLine (state, line) {
return state.src.slice(pos, max)
}

// Source position of `getLine(state, line).trim()`: besides the spaces and
// tabs counted by `tShift`, `trim()` removes Unicode whitespace such as U+00A0
function trimmedLineStart (state, line) {
return state.bMarks[line] + state.tShift[line] + trimLeftOffset(getLine(state, line))
}

// Sets the positions of the open, inline and close tokens of a row's cells.
//
// - The open token covers the `|` before its cell (size 1), or sits at the
// start of the cell (size 0) for the first cell of a row without a leading `|`.
// - The close token sits on the `|` after its cell (size 0); the last cell of
// the row claims the trailing `|` (size 1), or sits at the end of the cell
// (size 0) if there is none.
// - Autocompleted cells (missing from the row): all three at the end of the
// line, size 0.
//
// `cells` lists [open, inline, close] for each cell, `columns` the cells of
// the row once the empty first and last columns of a leading and trailing `|`
// are dropped.
function setCellPositions (state, line, cells, columns, hasLeadingPipe, hasTrailingPipe) {
let pos = trimmedLineStart(state, line)
if (hasLeadingPipe) { pos++ }

for (let i = 0; i < cells.length; i++) {
const [open, inline, close] = cells[i]

if (i >= columns.length) {
const eol = state.eMarks[line]
open.position = inline.position = close.position = eol
open.size = close.size = 0
continue
}

if (i === 0 && !hasLeadingPipe) {
open.position = pos
open.size = 0
} else {
open.position = pos - 1
open.size = 1
}

inline.position = pos + trimLeftOffset(columns[i])
pos += sourceCellLength(columns[i])

close.position = pos
close.size = (i === columns.length - 1 && hasTrailingPipe) ? 1 : 0
pos++
}
}

// Length of a cell in the source: every `|` left in a cell was `\|`
function sourceCellLength (cell) {
let length = cell.length
Expand Down Expand Up @@ -128,8 +178,10 @@ export default function table (state, startLine, endLine, silent) {
if (lineText.indexOf('|') === -1) { return false }
if (state.sCount[startLine] - state.blkIndent >= 4) { return false }
columns = escapedSplit(lineText)
if (columns.length && columns[0] === '') columns.shift()
if (columns.length && columns[columns.length - 1] === '') columns.pop()
let hasLeadingPipe = columns.length > 0 && columns[0] === ''

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these four lines are repeated at L265–268 for body rows. A small helper (e.g. splitRow(lineText) → { columns, hasLeadingPipe, hasTrailingPipe }) would keep the header and body rows from drifting apart. Not blocking.

if (hasLeadingPipe) columns.shift()
let hasTrailingPipe = columns.length > 0 && columns[columns.length - 1] === ''
if (hasTrailingPipe) columns.pop()

// header row will define an amount of columns in the entire table,
// and align row should be exactly the same (the rest of the rows can differ)
Expand Down Expand Up @@ -161,14 +213,11 @@ export default function table (state, startLine, endLine, silent) {
token_htro.size = 0
token_htro.position = state.bMarks[startLine]

let columnVIndex = state.bMarks[startLine] + state.tShift[startLine]
let cells = []

for (let i = 0; i < columns.length; i++) {
const token_ho = state.push('th_open', 'th', 1)
token_ho.map = [startLine, startLine + 1]
token_ho.size = 1
token_ho.position = columnVIndex
columnVIndex += 1

if (aligns[i]) {
token_ho.attrs = [['style', 'text-align:' + aligns[i]]]
Expand All @@ -177,21 +226,15 @@ export default function table (state, startLine, endLine, silent) {
const token_il = state.push('inline', '', 0)
token_il.content = columns[i].trim()
token_il.children = []
token_il.position = columnVIndex + trimLeftOffset(columns[i])
token_il.size = token_il.content.length
token_il.map = [nextLine, nextLine + 1]
columnVIndex += sourceCellLength(columns[i])
token_il.map = [startLine, startLine + 1]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: header inlines now get map [startLine, startLine+1]. The inline state takes startLine from map[0], so header cells now read lineOffsets of the header line instead of the delimiter line. That is the right line, and it matches th_open.map. Plugins that relied on the old value will see the change; the CHANGELOG covers it.


const token_hc = state.push('th_close', 'th', -1)
token_hc.position = columnVIndex

// Last column?
if (i === (columns.length - 1)) {
token_hc.size = 1
columnVIndex += 1
}
cells.push([token_ho, token_il, token_hc])
}

setCellPositions(state, startLine, cells, columns, hasLeadingPipe, hasTrailingPipe)

const token_htrc = state.push('tr_close', 'tr', -1)
token_htrc.size = 0
token_htrc.position = state.eMarks[startLine]
Expand Down Expand Up @@ -219,8 +262,10 @@ export default function table (state, startLine, endLine, silent) {
if (!lineText) { break }
if (state.sCount[nextLine] - state.blkIndent >= 4) { break }
columns = escapedSplit(lineText)
if (columns.length && columns[0] === '') columns.shift()
if (columns.length && columns[columns.length - 1] === '') columns.pop()
hasLeadingPipe = columns.length > 0 && columns[0] === ''
if (hasLeadingPipe) columns.shift()
hasTrailingPipe = columns.length > 0 && columns[columns.length - 1] === ''
if (hasTrailingPipe) columns.pop()

// note: autocomplete count can be negative if user specifies more columns than header,
// but that does not affect intended use (which is limiting expansion)
Expand All @@ -239,36 +284,27 @@ export default function table (state, startLine, endLine, silent) {
token_tro.size = 0
token_tro.position = state.bMarks[nextLine]

columnVIndex = state.bMarks[nextLine] + state.tShift[nextLine]
cells = []

for (let i = 0; i < columnCount; i++) {
const token_tdo = state.push('td_open', 'td', 1)
token_tdo.size = 1
token_tdo.position = columnVIndex
columnVIndex++

if (aligns[i]) {
token_tdo.attrs = [['style', 'text-align:' + aligns[i]]]
}

const originalContent = columns[i] || ''

const token_il = state.push('inline', '', 0)
token_il.content = columns[i] ? columns[i].trim() : ''
token_il.children = []
token_il.size = token_il.content.length
token_il.position = columnVIndex + trimLeftOffset(originalContent)
token_il.map = [nextLine, nextLine + 1]
columnVIndex += sourceCellLength(originalContent)

const token_tdc = state.push('td_close', 'td', -1)
token_tdc.position = columnVIndex

// Last column?
if (i === (columns.length - 1)) {
token_tdc.size = 1
}
cells.push([token_tdo, token_il, token_tdc])
}

setCellPositions(state, nextLine, cells, columns, hasLeadingPipe, hasTrailingPipe)

const token_trc = state.push('tr_close', 'tr', -1)
token_trc.size = 0
token_trc.position = state.eMarks[nextLine]
Expand Down
Loading
Loading