Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Table Recognition So It Does Not Match Table Rows with Just Dashes in Them #1248

Merged
merged 2 commits into from
Dec 24, 2024
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
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
Loading