Skip to content

Commit

Permalink
Merge pull request #1001 from pjkaufman/master
Browse files Browse the repository at this point in the history
Fix Custom Table Parser Not Respecting Escaped Pipes
  • Loading branch information
pjkaufman authored Jan 27, 2024
2 parents d3515fe + e695410 commit 5ed5733
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
16 changes: 15 additions & 1 deletion __tests__/get-all-tables-in-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const getTablesInTextTestCases: tablesInTextTestCase[] = [
---
date: 06/16/2022
type: topic
tags:
tags:
keywords: []
status: WIP
---
Expand All @@ -218,6 +218,20 @@ const getTablesInTextTestCases: tablesInTextTestCase[] = [
expectedTablesInText: 0,
expectedPositions: [],
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/999
name: 'handle tables with a escaped pipes in them',
text: dedent`
| 1 | 2 |
| --- | --- |
| 3 | 4 |
| [[2024-01-25\\|Thursday]] | [[2024-01-26\\|Friday]] |
| --- | --- |
| 3 | 4 |
`,
expectedTablesInText: 2,
expectedPositions: [{startIndex: 35, endIndex: 112}, {startIndex: 0, endIndex: 33}],
},
];

describe('Get All Tables in Text', () => {
Expand Down
21 changes: 21 additions & 0 deletions __tests__/paragraph-blank-lines.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,5 +390,26 @@ ruleTest({
[^5]: D
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/999
testName: 'Make sure that tables with escaped pipes in them are ignored',
before: dedent`
| 1 | 2 |
| --- | --- |
| 3 | 4 |
| [[2024-01-25\\|Thursday]] | [[2024-01-26\\|Friday]] |
| --- | --- |
| 3 | 4 |
`,
after: dedent`
| 1 | 2 |
| --- | --- |
| 3 | 4 |
| [[2024-01-25\\|Thursday]] | [[2024-01-26\\|Friday]] |
| --- | --- |
| 3 | 4 |
`,
},
],
});
25 changes: 24 additions & 1 deletion src/utils/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ export function getAllTablesInText(text: string): {startIndex: number, endIndex:

// if the delimiter row and the first row do not have the same amount of cells,
// we are not dealing with a table
if (firstLine.split('|').length !== delimiterLine.split('|').length) {
if (countTableDelimiters(firstLine) !== countTableDelimiters(delimiterLine)) {
continue;
}

Expand Down Expand Up @@ -958,6 +958,29 @@ function isInvalidTableSeparatorRow(fullRow: string, separatorMatch: string): bo
return /[^\s>]/.test(nonSeparatorContent);
}

function countTableDelimiters(line: string): number {
let previousCharIsEscapeChar = false;
let numEscapeCharsInARow = 0;
let numDelimiters = 0;
let currentChar = '';
for (let i = 0; i < line.length; i++) {
currentChar = line[i];
if (currentChar === '\\') {
numEscapeCharsInARow++;
previousCharIsEscapeChar = numEscapeCharsInARow % 2 == 1;
} else {
numEscapeCharsInARow = 0;
if (currentChar === '|' && !previousCharIsEscapeChar) {
numDelimiters++;
}

previousCharIsEscapeChar = false;
}
}

return numDelimiters;
}

export function getAllCustomIgnoreSectionsInText(text: string): {startIndex: number, endIndex: number}[] {
let iteratorIndex = 0;

Expand Down

0 comments on commit 5ed5733

Please sign in to comment.