Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions __tests__/file-name-heading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,16 @@ ruleTest({
fileName: 'Test note',
},
},
{ // accounts for https://github.com/platers/obsidian-linter/issues/1426
testName: 'Escapes the markdown special characters in the file name',
before: '',
after: dedent`
# Escape \\[\\_\\]
${''}
`,
options: {
fileName: 'Escape [_]',
},
},
],
});
4 changes: 2 additions & 2 deletions src/rules/file-name-heading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Options, rulesDict, RuleType} from '../rules';
import RuleBuilder, {ExampleBuilder, OptionBuilderBase} from './rule-builder';
import dedent from 'ts-dedent';
import {IgnoreTypes} from '../utils/ignore-types';
import {insert} from '../utils/strings';
import {escapeMarkdownSpecialCharacters, insert} from '../utils/strings';
import {App} from 'obsidian';
import {BooleanOption} from '../option';
import {ConfirmRuleDisableModal} from '../ui/modals/confirm-rule-disable-modal';
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class FileNameHeading extends RuleBuilder<FileNameHeadingOptions>
yaml_end =
yaml_end == -1 || !text.startsWith('---\n') ? 0 : yaml_end + 5;

let header = `# ${fileName}\n`;
let header = `# ${escapeMarkdownSpecialCharacters(fileName)}\n`;
if (text.length < yaml_end) {
header = '\n' + header;
}
Expand Down
15 changes: 15 additions & 0 deletions src/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,18 @@ export function parseCustomReplacements(text: string): Map<string, string> {

return customReplacements;
}

/**
* Escapes the markdown special characters in the provided text.
*
* @param {string} text The text to escape the markdown special characters in.
* @return {string} The text with the markdown special characters escaped.
*
* @example
* ```ts
* escapeMarkdownSpecialCharacters('Escape [_]'); // Escape \[\_\]
* ```
*/
export function escapeMarkdownSpecialCharacters(text: string): string {
return text.replace(/[\\[\]<>_*~=`$]/g, '\\$&');
}