Skip to content

Commit 350ae9f

Browse files
authored
Merge pull request #1345 from pjkaufman/master
Fix `YAML Title Alias` Adding and Leaving Blank YAML in Notes
2 parents 03af2fe + b63a370 commit 350ae9f

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

__tests__/yaml-title-alias.test.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,6 @@ ruleTest({
635635
# Filename
636636
`,
637637
after: dedent`
638-
---
639-
---
640638
# Filename
641639
`,
642640
options: {
@@ -1062,8 +1060,6 @@ ruleTest({
10621060
# Filename
10631061
`,
10641062
after: dedent`
1065-
---
1066-
---
10671063
# Filename
10681064
`,
10691065
options: {
@@ -1414,5 +1410,42 @@ ruleTest({
14141410
fileName: 'Filename',
14151411
},
14161412
},
1413+
{ // accounts for https://github.com/platers/obsidian-linter/issues/1293
1414+
testName: 'Make sure that a blank YAML block is not added when there is no value to be added by the rule',
1415+
before: dedent`
1416+
# Filename
1417+
`,
1418+
after: dedent`
1419+
# Filename
1420+
`,
1421+
options: {
1422+
aliasArrayStyle: NormalArrayFormats.MultiLine,
1423+
defaultEscapeCharacter: '"',
1424+
preserveExistingAliasesSectionStyle: true,
1425+
aliasHelperKey: 'aliases2',
1426+
keepAliasThatMatchesTheFilename: false,
1427+
fileName: 'Filename',
1428+
},
1429+
},
1430+
{ // relates to https://github.com/platers/obsidian-linter/issues/1293
1431+
testName: 'Make sure that a blank YAML block is not added when there is no value to be added by the rule and that extra space is removed before the first piece of content',
1432+
before: dedent`
1433+
---
1434+
---
1435+
${''}
1436+
# Filename
1437+
`,
1438+
after: dedent`
1439+
# Filename
1440+
`,
1441+
options: {
1442+
aliasArrayStyle: NormalArrayFormats.MultiLine,
1443+
defaultEscapeCharacter: '"',
1444+
preserveExistingAliasesSectionStyle: true,
1445+
aliasHelperKey: 'aliases2',
1446+
keepAliasThatMatchesTheFilename: false,
1447+
fileName: 'Filename',
1448+
},
1449+
},
14171450
],
14181451
});

src/rules/yaml-title-alias.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,17 @@ export default class YamlTitleAlias extends RuleBuilder<YamlTitleAliasOptions> {
112112
newYaml = setYamlSection(newYaml, aliasHelperKey, ` ${title}`);
113113
}
114114

115-
text = text.replace(`---\n${yaml}---`, `---\n${newYaml}---`);
115+
const oldYaml = `---\n${yaml}---`;
116+
let newYamlValue = `---\n${newYaml}---`;
117+
if (newYaml === '') {
118+
newYamlValue = '';
119+
}
120+
121+
text = text.replace(oldYaml, newYamlValue);
122+
123+
if (newYaml === '') {
124+
text = text.trimStart();
125+
}
116126

117127
return text;
118128
}

src/utils/strings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export function hashString53Bit(str: string, seed: number = 0): number {
359359
}
360360

361361
/**
362-
* Takes a string and converts string that have the string escaped form of escape characters such as new line, backspace,
362+
* Takes a string and converts string that have the string escaped form of escape characters such as new line,
363363
* form feed, carriage return, horizontal tab, and vertical tab and makes sure they are their escaped character values.
364364
* @param {string} val - The string to make sure has the escape characters as escape characters rather than a stringified form.
365365
* @return {string} The string with the escape characters converted to their escape character form.

0 commit comments

Comments
 (0)