Skip to content

Commit 1f62de7

Browse files
authored
Merge pull request #1269 from pjkaufman/master
Fix List Ignore Type Ignoring More than It Should when Sublists Are Present
2 parents 185ceaf + 9073c83 commit 1f62de7

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

__tests__/remove-multiple-spaces.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,21 @@ ruleTest({
239239
> > - e.g., \`Customer\` class might have \`getName\`, \`getAddress\`, but also \`sendEmail\` that sends an email to the customer → Weird; not a major responsibility of the customer → ==not cohesive
240240
`,
241241
},
242+
{ // accounts for https://github.com/platers/obsidian-linter/issues/1203
243+
testName: 'A nested callout/blockquote should not have list item proceeding space removed',
244+
before: dedent`
245+
- first item
246+
- xxxxxxxxxxxxxxxxxxxxxxxxx
247+
248+
test test
249+
`,
250+
after: dedent`
251+
- first item
252+
- xxxxxxxxxxxxxxxxxxxxxxxxx
253+
254+
test test
255+
`,
256+
},
242257
],
243258
});
244259

src/utils/ignore-types.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,13 @@ export function ignoreListOfTypes(ignoreTypes: IgnoreType[], text: string, func:
8181
* @return {string[]} The mdast nodes values replaced
8282
*/
8383
function replaceMdastType(text: string, placeholder: string, type: MDAstTypes): IgnoreResults {
84-
const positions: Position[] = getPositions(type, text);
84+
let positions: Position[] = getPositions(type, text);
8585
const replacedValues: string[] = [];
8686

87+
if (type === MDAstTypes.List) {
88+
positions = removeOverlappingPositions(positions);
89+
}
90+
8791
for (const position of positions) {
8892
const valueToReplace = text.substring(position.start.offset, position.end.offset);
8993
replacedValues.push(valueToReplace);
@@ -197,3 +201,22 @@ function replaceCustomIgnore(text: string, customIgnorePlaceholder: string): Ign
197201

198202
return {newText: text, replacedValues: replacedSections};
199203
}
204+
205+
function removeOverlappingPositions(positions: Position[]): Position[] {
206+
if (positions.length < 2) {
207+
return positions;
208+
}
209+
210+
let lastPosition: Position = positions.pop();
211+
let currentPosition: Position = null;
212+
const result: Position[] = [lastPosition];
213+
while (positions.length > 0) {
214+
currentPosition = positions.pop();
215+
if (lastPosition.start.offset >= currentPosition.end.offset || currentPosition.start.offset >= lastPosition.end.offset) {
216+
result.unshift(currentPosition);
217+
lastPosition = currentPosition;
218+
}
219+
}
220+
221+
return result;
222+
}

0 commit comments

Comments
 (0)