Skip to content

Commit

Permalink
Merge pull request #818 from pjkaufman/master
Browse files Browse the repository at this point in the history
Fix Moving Math Block Indicators to Their Own Line Breaking Math Blocks when Malformed with Content Between Them
  • Loading branch information
pjkaufman authored Jul 25, 2023
2 parents ba54a88 + 0241adc commit 15055a9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
26 changes: 26 additions & 0 deletions __tests__/move-math-block-indicators-to-own-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,31 @@ ruleTest({
$$
`,
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/813
testName: 'Math indicators that are malformed with content between them should be properly formatted without messing up the content of the other math blocks',
before: dedent`
$$
\\sqrt{(x_1-x_2)^2+(y_1-y_2)^2+\cdots+(n_1-n_2)^2}$$
- here is some text
$$
\\begin{aligned}
\\frac{n*(n+1)}{2}=\\frac{1000*1001}{2}=500500
\\end{aligned}
$$
text
`,
after: dedent`
$$
\\sqrt{(x_1-x_2)^2+(y_1-y_2)^2+\cdots+(n_1-n_2)^2}
$$
- here is some text
$$
\\begin{aligned}
\\frac{n*(n+1)}{2}=\\frac{1000*1001}{2}=500500
\\end{aligned}
$$
text
`,
},
],
});
5 changes: 5 additions & 0 deletions src/rules-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {getTextInLanguage} from './lang/helpers';
import CapitalizeHeadings from './rules/capitalize-headings';
import BlockquoteStyle from './rules/blockquote-style';
import {IgnoreTypes, ignoreListOfTypes} from './utils/ignore-types';
import MoveMathBlockIndicatorsToOwnLine from './rules/move-math-block-indicators-to-own-line';

export type RunLinterRulesOptions = {
oldText: string,
Expand Down Expand Up @@ -100,6 +101,10 @@ export class RulesRunner {
defaultEscapeCharacter: runOptions.settings.commonStyles.escapeCharacter,
});

[newText] = MoveMathBlockIndicatorsToOwnLine.applyIfEnabled(newText, runOptions.settings, this.disabledRules, {
minimumNumberOfDollarSignsToBeAMathBlock: runOptions.settings.commonStyles.minimumNumberOfDollarSignsToBeAMathBlock,
});

return newText;
}

Expand Down
1 change: 1 addition & 0 deletions src/rules/move-math-block-indicators-to-own-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class MoveMathBlockIndicatorsToOwnLine extends RuleBuilder<MoveMa
descriptionKey: 'rules.move-math-block-indicators-to-their-own-line.description',
type: RuleType.SPACING,
ruleIgnoreTypes: [IgnoreTypes.code, IgnoreTypes.inlineCode],
hasSpecialExecutionOrder: true,
});
}
get OptionsClass(): new () => MoveMathBlockIndicatorsToOwnLineOptions {
Expand Down
11 changes: 9 additions & 2 deletions src/utils/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,13 +777,20 @@ function breakMathBlockIntoMultipleBlocksIfNeedBe(mathBlock: string, numberOfDol
const mathBlockIndexes = [] as {startIndex: number, endIndex: number}[];

let matchCount = countInstances(mathBlock, mathBlockIndicator);
if (matchCount <= 3) {
if (matchCount <= 1) {
return [];
} else if (matchCount === 2) {
mathBlockIndexes.unshift({
startIndex: startIndexOfMathBlock,
endIndex: startIndexOfMathBlock + mathBlock.length,
});

return mathBlockIndexes;
} else if (matchCount === 3) {
mathBlockIndexes.unshift({
startIndex: startIndexOfMathBlock,
endIndex: startIndexOfMathBlock + mathBlock.indexOf(mathBlockIndicator, mathBlockIndicator.length) + mathBlockIndicator.length,
});
}

// if there is an odd amount of matches, remove one from the list so it is even
Expand All @@ -794,7 +801,7 @@ function breakMathBlockIntoMultipleBlocksIfNeedBe(mathBlock: string, numberOfDol
// pair the earliest matches together until there are no more pairs
let startIndex = startIndexOfMathBlock;
let startSearch = mathBlockIndicator.length;
while (matchCount > 2 ) {
while (matchCount > 2) {
const endOfIndex = mathBlock.indexOf(mathBlockIndicator, startSearch) + mathBlockIndicator.length;
mathBlockIndexes.unshift({
startIndex: startIndex,
Expand Down

0 comments on commit 15055a9

Please sign in to comment.