Skip to content

Commit

Permalink
limit the number of autocompleted cells in a table
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Mar 1, 2024
1 parent d07d585 commit a164209
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/rules_block/table.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import { isSpace } from '../common/utils.mjs'

// see https://github.com/markdown-it/markdown-it/issues/1000,
// both pulldown-cmark and commonmark-hs limit the number of cells this way to ~200k
const MAX_AUTOCOMPLETED_CELLS = 0x40000;

Check failure on line 7 in lib/rules_block/table.mjs

View workflow job for this annotation

GitHub Actions / test (18)

Extra semicolon

function getLine (state, line) {
const pos = state.bMarks[line] + state.tShift[line]
const max = state.eMarks[line]
Expand Down Expand Up @@ -157,6 +161,7 @@ export default function table (state, startLine, endLine, silent) {
state.push('thead_close', 'thead', -1)

let tbodyLines
let autocompletedCells = 0

for (nextLine = startLine + 2; nextLine < endLine; nextLine++) {
if (state.sCount[nextLine] < state.blkIndent) { break }
Expand All @@ -177,6 +182,11 @@ export default function table (state, startLine, endLine, silent) {
if (columns.length && columns[0] === '') columns.shift()
if (columns.length && columns[columns.length - 1] === '') 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)
autocompletedCells += columnCount - columns.length
if (autocompletedCells > MAX_AUTOCOMPLETED_CELLS) { break }

if (nextLine === startLine + 2) {
const token_tbo = state.push('tbody_open', 'tbody', 1)
token_tbo.map = tbodyLines = [startLine + 2, 0]
Expand Down

0 comments on commit a164209

Please sign in to comment.