Skip to content

Commit 1877e2b

Browse files
authored
Merge pull request #1400 from pjkaufman/master
Add Ability to Have a Blank Line Between Footnotes for `Move Footnote to Bottom`
2 parents fd9c311 + e739953 commit 1877e2b

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

__tests__/move-footnotes-to-the-bottom.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,5 +305,25 @@ ruleTest({
305305
[^3]: [3333](333)
306306
`,
307307
},
308+
{ // accounts for https://github.com/platers/obsidian-linter/issues/1392
309+
testName: 'Moving footnotes to the bottom of the file when including blank lines between footnotes should not change a file with footnotes at the end with blank lines between them already',
310+
before: dedent`
311+
This is the first footnote.[^1] This is the second.[^2]
312+
${''}
313+
[^1]: Hey, I am a footnote!
314+
${''}
315+
[^2]: Me too!
316+
`,
317+
after: dedent`
318+
This is the first footnote.[^1] This is the second.[^2]
319+
${''}
320+
[^1]: Hey, I am a footnote!
321+
${''}
322+
[^2]: Me too!
323+
`,
324+
options: {
325+
includeBlankLineBetweenFootnotes: true,
326+
},
327+
},
308328
],
309329
});

src/lang/locale/en.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@ export default {
532532
'move-footnotes-to-the-bottom': {
533533
'name': 'Move Footnotes to the bottom',
534534
'description': 'Move all footnotes to the bottom of the document and makes sure they are sorted based on the order they are referenced in the file\'s body.',
535+
'include-blank-line-between-footnotes': {
536+
'name': 'Include Blank Line Between Footnotes',
537+
'description': 'Includes a blank line between footnotes when enabled.',
538+
},
535539
},
536540
// move-math-block-indicators-to-their-own-line.ts
537541
'move-math-block-indicators-to-their-own-line': {

src/rules/move-footnotes-to-the-bottom.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {Options, RuleType} from '../rules';
2-
import RuleBuilder, {ExampleBuilder, OptionBuilderBase} from './rule-builder';
2+
import RuleBuilder, {BooleanOptionBuilder, ExampleBuilder, OptionBuilderBase} from './rule-builder';
33
import dedent from 'ts-dedent';
44
import {IgnoreTypes} from '../utils/ignore-types';
55
import {moveFootnotesToEnd} from '../utils/mdast';
66

7-
class MoveFootnotesToTheBottomOptions implements Options {}
7+
class MoveFootnotesToTheBottomOptions implements Options {
8+
includeBlankLineBetweenFootnotes?: boolean = false;
9+
}
810

911
@RuleBuilder.register
1012
export default class MoveFootnotesToTheBottom extends RuleBuilder<MoveFootnotesToTheBottomOptions> {
@@ -20,7 +22,7 @@ export default class MoveFootnotesToTheBottom extends RuleBuilder<MoveFootnotesT
2022
return MoveFootnotesToTheBottomOptions;
2123
}
2224
apply(text: string, options: MoveFootnotesToTheBottomOptions): string {
23-
return moveFootnotesToEnd(text);
25+
return moveFootnotesToEnd(text, options.includeBlankLineBetweenFootnotes);
2426
}
2527
get exampleBuilders(): ExampleBuilder<MoveFootnotesToTheBottomOptions>[] {
2628
return [
@@ -46,9 +48,42 @@ export default class MoveFootnotesToTheBottom extends RuleBuilder<MoveFootnotesT
4648
[^2]: second footnote
4749
`,
4850
}),
51+
new ExampleBuilder({
52+
description: 'Moving footnotes to the bottom with including a blank line between footnotes',
53+
before: dedent`
54+
Lorem ipsum, consectetur adipiscing elit. [^1] Donec dictum turpis quis ipsum pellentesque.
55+
${''}
56+
[^1]: first footnote
57+
${''}
58+
Quisque lorem est, fringilla sed enim at, sollicitudin lacinia nisi.[^2]
59+
[^2]: second footnote
60+
${''}
61+
Maecenas malesuada dignissim purus ac volutpat.
62+
`,
63+
after: dedent`
64+
Lorem ipsum, consectetur adipiscing elit. [^1] Donec dictum turpis quis ipsum pellentesque.
65+
${''}
66+
Quisque lorem est, fringilla sed enim at, sollicitudin lacinia nisi.[^2]
67+
Maecenas malesuada dignissim purus ac volutpat.
68+
${''}
69+
[^1]: first footnote
70+
${''}
71+
[^2]: second footnote
72+
`,
73+
options: {
74+
includeBlankLineBetweenFootnotes: true,
75+
},
76+
}),
4977
];
5078
}
5179
get optionBuilders(): OptionBuilderBase<MoveFootnotesToTheBottomOptions>[] {
52-
return [];
80+
return [
81+
new BooleanOptionBuilder({
82+
OptionsClass: MoveFootnotesToTheBottomOptions,
83+
nameKey: 'rules.move-footnotes-to-the-bottom.include-blank-line-between-footnotes.name',
84+
descriptionKey: 'rules.move-footnotes-to-the-bottom.include-blank-line-between-footnotes.description',
85+
optionsKey: 'includeBlankLineBetweenFootnotes',
86+
}),
87+
];
5388
}
5489
}

src/utils/mdast.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,10 @@ function getHeaderTextPositions(text: string): PositionPlusText[] {
186186
/**
187187
* Moves footnote declarations to the end of the document.
188188
* @param {string} text The text to move footnotes in
189+
* @param {boolean} includeBlankLinesBetweenFootnotes Whether to have a blank line between footnotes
189190
* @return {string} The text with footnote declarations moved to the end
190191
*/
191-
export function moveFootnotesToEnd(text: string): string {
192+
export function moveFootnotesToEnd(text: string, includeBlankLinesBetweenFootnotes: boolean): string {
192193
const positions: Position[] = getPositions(MDAstTypes.Footnote, text);
193194
let footnotes: string[] = [];
194195

@@ -273,10 +274,17 @@ export function moveFootnotesToEnd(text: string): string {
273274

274275
// Add the footnotes to the end of the document
275276
if (footnotes.length > 0) {
276-
text = text.trimEnd() + '\n';
277+
text = text.trimEnd();
277278
}
279+
let whitespaceBetweenFootnotes = '\n';
280+
if (includeBlankLinesBetweenFootnotes) {
281+
whitespaceBetweenFootnotes = '\n\n';
282+
} else {
283+
text += '\n';
284+
}
285+
278286
for (const footnote of footnotes) {
279-
text += '\n' + footnote;
287+
text += whitespaceBetweenFootnotes + footnote;
280288
}
281289

282290
return text;

0 commit comments

Comments
 (0)