Skip to content

Commit a88b2e9

Browse files
authored
Merge pull request #1200 from pjkaufman/master
Fix `Add Blank Line After YAML` Only Working When YAML Already Exists
2 parents 0a35e35 + d1cc19c commit a88b2e9

File tree

11 files changed

+101
-3
lines changed

11 files changed

+101
-3
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Use Node.js
1919
uses: actions/setup-node@v3
2020
with:
21-
node-version: "18.x"
21+
node-version: "16.x"
2222

2323
- name: Build plugin and minfy css
2424
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ yarn.lock
4242
!__mocks__/*.js
4343
!/.eslintrc.js
4444
!babel.config.js
45+
!postcss.config.js

__integration__/yaml-rule.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import TestLinterPlugin, {IntegrationTestCase} from './main.test';
2-
import {Editor} from 'obsidian';
2+
import {Editor, TFile} from 'obsidian';
3+
import moment from 'moment';
34

45
function addBlankLineAfterSetup(plugin: TestLinterPlugin, _: Editor): Promise<void> {
56
plugin.plugin.settings.ruleConfigs['add-blank-line-after-yaml'] = {
@@ -9,6 +10,53 @@ function addBlankLineAfterSetup(plugin: TestLinterPlugin, _: Editor): Promise<vo
910
return;
1011
}
1112

13+
function addBlankLineAfterYAMLConflictWithYAMLTimestampInsert(plugin: TestLinterPlugin): Promise<void> {
14+
plugin.plugin.settings.ruleConfigs['add-blank-line-after-yaml'] = {
15+
'enabled': true,
16+
};
17+
plugin.plugin.settings.ruleConfigs['yaml-timestamp'] = {
18+
'enabled': true,
19+
'date-created': true,
20+
'date-created-key': 'created',
21+
'date-created-source-of-truth': 'file system',
22+
'date-modified': true,
23+
'date-modified-key': 'last_modified',
24+
'format': 'YYYY-MM-DD',
25+
};
26+
27+
return;
28+
}
29+
30+
function addBlankLineAfterYAMLConflictWithYAMLTimestampInsertExpectedTextModifications(text: string, file: TFile):string {
31+
text = text.replace('{{created_date}}', moment(file.stat.ctime ?? '').format('YYYY-MM-DD'));
32+
text = text.replace('{{modified_date}}', moment().format('YYYY-MM-DD'));
33+
34+
return text;
35+
}
36+
37+
function addBlankLineAfterYAMLConflictWithYAMLTimestampUpdate(plugin: TestLinterPlugin): Promise<void> {
38+
plugin.plugin.settings.ruleConfigs['add-blank-line-after-yaml'] = {
39+
'enabled': true,
40+
};
41+
plugin.plugin.settings.ruleConfigs['yaml-timestamp'] = {
42+
'enabled': true,
43+
'date-created': false,
44+
'date-modified': true,
45+
'date-modified-key': 'last_modified',
46+
'date-modified-source-of-truth': 'user or Linter edits',
47+
'format': 'YYYY-MM-DD',
48+
};
49+
50+
return;
51+
}
52+
53+
function addBlankLineAfterYAMLConflictWithYAMLTimestampUpdateExpectedTextModifications(text: string):string {
54+
text = text.replace('{{modified_date}}', moment().format('YYYY-MM-DD'));
55+
56+
return text;
57+
}
58+
59+
1260
export const obsidianYAMLRuleTestCases: IntegrationTestCase[] = [
1361
{
1462
name: 'Updating a file with no yaml for adding blank lines after yaml should do nothing',
@@ -20,4 +68,16 @@ export const obsidianYAMLRuleTestCases: IntegrationTestCase[] = [
2068
filePath: 'yaml-rules/add-blank-line-after-yaml/yaml.md',
2169
setup: addBlankLineAfterSetup,
2270
},
71+
{
72+
name: 'Updating a file with no yaml where YAML Timestamp Adds a Date Created, should properly update the YAML with a blank line after the frontmatter',
73+
filePath: 'yaml-rules/add-blank-line-after-yaml/no-yaml-timestamp-addition.md',
74+
setup: addBlankLineAfterYAMLConflictWithYAMLTimestampInsert,
75+
modifyExpected: addBlankLineAfterYAMLConflictWithYAMLTimestampInsertExpectedTextModifications,
76+
},
77+
{
78+
name: 'Updating a file with yaml where YAML Timestamp will only be updated if a change is made to the file, should properly add the missing blank line and update the date modified timestamp',
79+
filePath: 'yaml-rules/add-blank-line-after-yaml/yaml-timestamp-update.md',
80+
setup: addBlankLineAfterYAMLConflictWithYAMLTimestampUpdate,
81+
modifyExpected: addBlankLineAfterYAMLConflictWithYAMLTimestampUpdateExpectedTextModifications,
82+
},
2383
];

postcss.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
plugins: [
3+
require('cssnano')({
4+
preset: 'default',
5+
}),
6+
],
7+
};

src/lang/locale/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export default {
152152
},
153153
'lint-on-file-change': {
154154
'name': 'Lint on Focused File Change',
155-
'description': 'When the a file is closed or a new file is swapped to, the previous file is linted.',
155+
'description': 'When a file is closed or a new file is swapped to, the previous file is linted.',
156156
},
157157
'display-lint-on-file-change-message': {
158158
'name': 'Display Lint on File Change Message',

src/rules-runner.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import {LinterSettings} from './settings-data';
3030
import TrailingSpaces from './rules/trailing-spaces';
3131
import {CustomAutoCorrectContent} from './ui/linter-components/auto-correct-files-picker-option';
3232
import AutoCorrectCommonMisspellings from './rules/auto-correct-common-misspellings';
33+
import {yamlRegex} from './utils/regex';
34+
import AddBlankLineAfterYAML from './rules/add-blank-line-after-yaml';
3335

3436
export type RunLinterRulesOptions = {
3537
oldText: string,
@@ -169,6 +171,11 @@ export class RulesRunner {
169171

170172
[newText] = TrailingSpaces.applyIfEnabled(newText, runOptions.settings, this.disabledRules);
171173

174+
const yaml = newText.match(yamlRegex);
175+
if (yaml != null) {
176+
[newText] = AddBlankLineAfterYAML.applyIfEnabled(newText, runOptions.settings, this.disabledRules);
177+
}
178+
172179
let currentTime = runOptions.getCurrentTime();
173180
// run YAML timestamp at the end to help determine if something has changed
174181
let isYamlTimestampEnabled;
@@ -180,6 +187,10 @@ export class RulesRunner {
180187
locale: runOptions.momentLocale,
181188
});
182189

190+
if (yaml === null) {
191+
[newText] = AddBlankLineAfterYAML.applyIfEnabled(newText, runOptions.settings, this.disabledRules);
192+
}
193+
183194
const yamlTimestampOptions = YamlTimestamp.getRuleOptions(runOptions.settings);
184195

185196
currentTime = runOptions.getCurrentTime();
@@ -191,6 +202,7 @@ export class RulesRunner {
191202
yamlTimestampDateModifiedEnabled: isYamlTimestampEnabled && yamlTimestampOptions.dateModified,
192203
dateModifiedKey: yamlTimestampOptions.dateModifiedKey,
193204
});
205+
194206
timingEnd(postRuleLogText);
195207
timingEnd(getTextInLanguage('logs.rule-running'));
196208
return newText;

src/rules/add-blank-line-after-yaml.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export default class AddBlankLineAfterYAML extends RuleBuilder<AddBlankLineAfter
1313
nameKey: 'rules.add-blank-line-after-yaml.name',
1414
descriptionKey: 'rules.add-blank-line-after-yaml.description',
1515
type: RuleType.YAML,
16+
// needs to run before YAML timestamp if the YAML is present, but after it otherwise
17+
hasSpecialExecutionOrder: true,
1618
});
1719
}
1820
get OptionsClass(): new () => AddBlankLineAfterYAMLOptions {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
created: {{created_date}}
3+
last_modified: {{modified_date}}
4+
---
5+
6+
Content here.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Content here.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
last_modified: {{modified_date}}
3+
---
4+
5+
Content here.

0 commit comments

Comments
 (0)