Skip to content

Commit f744752

Browse files
authored
Merge pull request #1248 from pjkaufman/master
Fix Table Recognition So It Does Not Match Table Rows with Just Dashes in Them
2 parents cea40b8 + 98e129a commit f744752

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

__tests__/empty-line-around-tables.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,40 @@ ruleTest({
121121
content
122122
`,
123123
},
124+
{ // accounts for https://github.com/platers/obsidian-linter/issues/1235
125+
testName: 'Make sure that we do not break a table apart when it rows with just dashes in them',
126+
before: dedent`
127+
| test |
128+
|:-----:|
129+
| --- |
130+
| one |
131+
| two |
132+
| --- |
133+
| --- |
134+
| three |
135+
| --- |
136+
| --- |
137+
| --- |
138+
| --- |
139+
| four |
140+
| five |
141+
`,
142+
after: dedent`
143+
| test |
144+
|:-----:|
145+
| --- |
146+
| one |
147+
| two |
148+
| --- |
149+
| --- |
150+
| three |
151+
| --- |
152+
| --- |
153+
| --- |
154+
| --- |
155+
| four |
156+
| five |
157+
`,
158+
},
124159
],
125160
});

__tests__/get-all-tables-in-text.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,27 @@ const getTablesInTextTestCases: tablesInTextTestCase[] = [
232232
expectedTablesInText: 2,
233233
expectedPositions: [{startIndex: 35, endIndex: 112}, {startIndex: 0, endIndex: 33}],
234234
},
235+
{ // accounts for https://github.com/platers/obsidian-linter/issues/1235
236+
name: 'handle tables with --- in a row',
237+
text: dedent`
238+
| test |
239+
|:-----:|
240+
| --- |
241+
| one |
242+
| two |
243+
| --- |
244+
| --- |
245+
| three |
246+
| --- |
247+
| --- |
248+
| --- |
249+
| --- |
250+
| four |
251+
| five |
252+
`,
253+
expectedTablesInText: 1,
254+
expectedPositions: [{startIndex: 0, endIndex: 139}],
255+
},
235256
];
236257

237258
describe('Get All Tables in Text', () => {

src/utils/mdast.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,17 @@ export function getAllTablesInText(text: string): {startIndex: number, endIndex:
10001000
continue;
10011001
}
10021002

1003+
// need to check that two lines before the separator line does not start and end with a pipe
1004+
if (startOfPreviousLine !== 0) {
1005+
const startOfTwoLinesPrior = getStartOfLineIndex(text, startOfPreviousLine - 1);
1006+
const twoLinesPrior = text.substring(startOfTwoLinesPrior, startOfPreviousLine - 1);
1007+
if (twoLinesPrior.startsWith('|') || twoLinesPrior.endsWith('|')) {
1008+
// the match is at best a row in a table
1009+
continue;
1010+
}
1011+
}
1012+
1013+
10031014
let end = match.index + match[0].length;
10041015

10051016
if (end >= text.length - 1) {

0 commit comments

Comments
 (0)