From 35baab8db3b494e220672904aff99ca1f4ec7c05 Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Tue, 24 Dec 2024 12:59:21 -0500 Subject: [PATCH 1/2] fix tables with just dashes in the rows getting misidentified by the Linter logic --- __tests__/empty-line-around-tables.test.ts | 35 ++++++++++++++++++++++ __tests__/get-all-tables-in-text.test.ts | 21 +++++++++++++ src/utils/mdast.ts | 11 +++++++ 3 files changed, 67 insertions(+) diff --git a/__tests__/empty-line-around-tables.test.ts b/__tests__/empty-line-around-tables.test.ts index ced0a506..77527290 100644 --- a/__tests__/empty-line-around-tables.test.ts +++ b/__tests__/empty-line-around-tables.test.ts @@ -121,5 +121,40 @@ ruleTest({ content `, }, + { // accounts for https://github.com/platers/obsidian-linter/issues/1235 + testName: 'Make sure that we do not break a table apart when it rows with just dashes in them', + before: dedent` + | test | + |:-----:| + | --- | + | one | + | two | + | --- | + | --- | + | three | + | --- | + | --- | + | --- | + | --- | + | four | + | five | + `, + after: dedent` + | test | + |:-----:| + | --- | + | one | + | two | + | --- | + | --- | + | three | + | --- | + | --- | + | --- | + | --- | + | four | + | five | + `, + }, ], }); diff --git a/__tests__/get-all-tables-in-text.test.ts b/__tests__/get-all-tables-in-text.test.ts index 2d121906..13967f17 100644 --- a/__tests__/get-all-tables-in-text.test.ts +++ b/__tests__/get-all-tables-in-text.test.ts @@ -232,6 +232,27 @@ const getTablesInTextTestCases: tablesInTextTestCase[] = [ expectedTablesInText: 2, expectedPositions: [{startIndex: 35, endIndex: 112}, {startIndex: 0, endIndex: 33}], }, + { // accounts for https://github.com/platers/obsidian-linter/issues/1235 + name: 'handle tables with --- in a row', + text: dedent` + | test | + |:-----:| + | --- | + | one | + | two | + | --- | + | --- | + | three | + | --- | + | --- | + | --- | + | --- | + | four | + | five | + `, + expectedTablesInText: 1, + expectedPositions: [{startIndex: 0, endIndex: 140}], + }, ]; describe('Get All Tables in Text', () => { diff --git a/src/utils/mdast.ts b/src/utils/mdast.ts index a39232f2..4ac92110 100644 --- a/src/utils/mdast.ts +++ b/src/utils/mdast.ts @@ -1000,6 +1000,17 @@ export function getAllTablesInText(text: string): {startIndex: number, endIndex: continue; } + // need to check that two lines before the separator line does not start and end with a pipe + if (startOfPreviousLine !== 0) { + const startOfTwoLinesPrior = getStartOfLineIndex(text, startOfPreviousLine - 1); + const twoLinesPrior = text.substring(startOfTwoLinesPrior, startOfPreviousLine - 1); + if (twoLinesPrior.startsWith('|') || twoLinesPrior.endsWith('|')) { + // the match is at best a row in a table + continue; + } + } + + let end = match.index + match[0].length; if (end >= text.length - 1) { From 98e129af847d01919990ecb88b0a08d5e04240dc Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Tue, 24 Dec 2024 13:00:57 -0500 Subject: [PATCH 2/2] fix UT --- __tests__/get-all-tables-in-text.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/get-all-tables-in-text.test.ts b/__tests__/get-all-tables-in-text.test.ts index 13967f17..9c1e196b 100644 --- a/__tests__/get-all-tables-in-text.test.ts +++ b/__tests__/get-all-tables-in-text.test.ts @@ -251,7 +251,7 @@ const getTablesInTextTestCases: tablesInTextTestCase[] = [ | five | `, expectedTablesInText: 1, - expectedPositions: [{startIndex: 0, endIndex: 140}], + expectedPositions: [{startIndex: 0, endIndex: 139}], }, ];