Skip to content

Commit

Permalink
Merge pull request #1248 from pjkaufman/master
Browse files Browse the repository at this point in the history
Fix Table Recognition So It Does Not Match Table Rows with Just Dashes in Them
  • Loading branch information
pjkaufman authored Dec 24, 2024
2 parents cea40b8 + 98e129a commit f744752
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
35 changes: 35 additions & 0 deletions __tests__/empty-line-around-tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
`,
},
],
});
21 changes: 21 additions & 0 deletions __tests__/get-all-tables-in-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: 139}],
},
];

describe('Get All Tables in Text', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f744752

Please sign in to comment.