Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Add Blank Line After YAML Only Working When YAML Already Exists #1200

Merged
merged 4 commits into from
Oct 21, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: "18.x"
node-version: "16.x"

- name: Build plugin and minfy css
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ yarn.lock
!__mocks__/*.js
!/.eslintrc.js
!babel.config.js
!postcss.config.js
62 changes: 61 additions & 1 deletion __integration__/yaml-rule.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import TestLinterPlugin, {IntegrationTestCase} from './main.test';
import {Editor} from 'obsidian';
import {Editor, TFile} from 'obsidian';
import moment from 'moment';

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

function addBlankLineAfterYAMLConflictWithYAMLTimestampInsert(plugin: TestLinterPlugin): Promise<void> {
plugin.plugin.settings.ruleConfigs['add-blank-line-after-yaml'] = {
'enabled': true,
};
plugin.plugin.settings.ruleConfigs['yaml-timestamp'] = {
'enabled': true,
'date-created': true,
'date-created-key': 'created',
'date-created-source-of-truth': 'file system',
'date-modified': true,
'date-modified-key': 'last_modified',
'format': 'YYYY-MM-DD',
};

return;
}

function addBlankLineAfterYAMLConflictWithYAMLTimestampInsertExpectedTextModifications(text: string, file: TFile):string {
text = text.replace('{{created_date}}', moment(file.stat.ctime ?? '').format('YYYY-MM-DD'));
text = text.replace('{{modified_date}}', moment().format('YYYY-MM-DD'));

return text;
}

function addBlankLineAfterYAMLConflictWithYAMLTimestampUpdate(plugin: TestLinterPlugin): Promise<void> {
plugin.plugin.settings.ruleConfigs['add-blank-line-after-yaml'] = {
'enabled': true,
};
plugin.plugin.settings.ruleConfigs['yaml-timestamp'] = {
'enabled': true,
'date-created': false,
'date-modified': true,
'date-modified-key': 'last_modified',
'date-modified-source-of-truth': 'user or Linter edits',
'format': 'YYYY-MM-DD',
};

return;
}

function addBlankLineAfterYAMLConflictWithYAMLTimestampUpdateExpectedTextModifications(text: string):string {
text = text.replace('{{modified_date}}', moment().format('YYYY-MM-DD'));

return text;
}


export const obsidianYAMLRuleTestCases: IntegrationTestCase[] = [
{
name: 'Updating a file with no yaml for adding blank lines after yaml should do nothing',
Expand All @@ -20,4 +68,16 @@ export const obsidianYAMLRuleTestCases: IntegrationTestCase[] = [
filePath: 'yaml-rules/add-blank-line-after-yaml/yaml.md',
setup: addBlankLineAfterSetup,
},
{
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',
filePath: 'yaml-rules/add-blank-line-after-yaml/no-yaml-timestamp-addition.md',
setup: addBlankLineAfterYAMLConflictWithYAMLTimestampInsert,
modifyExpected: addBlankLineAfterYAMLConflictWithYAMLTimestampInsertExpectedTextModifications,
},
{
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',
filePath: 'yaml-rules/add-blank-line-after-yaml/yaml-timestamp-update.md',
setup: addBlankLineAfterYAMLConflictWithYAMLTimestampUpdate,
modifyExpected: addBlankLineAfterYAMLConflictWithYAMLTimestampUpdateExpectedTextModifications,
},
];
7 changes: 7 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
plugins: [
require('cssnano')({
preset: 'default',
}),
],
};
2 changes: 1 addition & 1 deletion src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default {
},
'lint-on-file-change': {
'name': 'Lint on Focused File Change',
'description': 'When the a file is closed or a new file is swapped to, the previous file is linted.',
'description': 'When a file is closed or a new file is swapped to, the previous file is linted.',
},
'display-lint-on-file-change-message': {
'name': 'Display Lint on File Change Message',
Expand Down
12 changes: 12 additions & 0 deletions src/rules-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {LinterSettings} from './settings-data';
import TrailingSpaces from './rules/trailing-spaces';
import {CustomAutoCorrectContent} from './ui/linter-components/auto-correct-files-picker-option';
import AutoCorrectCommonMisspellings from './rules/auto-correct-common-misspellings';
import {yamlRegex} from './utils/regex';
import AddBlankLineAfterYAML from './rules/add-blank-line-after-yaml';

export type RunLinterRulesOptions = {
oldText: string,
Expand Down Expand Up @@ -169,6 +171,11 @@ export class RulesRunner {

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

const yaml = newText.match(yamlRegex);
if (yaml != null) {
[newText] = AddBlankLineAfterYAML.applyIfEnabled(newText, runOptions.settings, this.disabledRules);
}

let currentTime = runOptions.getCurrentTime();
// run YAML timestamp at the end to help determine if something has changed
let isYamlTimestampEnabled;
Expand All @@ -180,6 +187,10 @@ export class RulesRunner {
locale: runOptions.momentLocale,
});

if (yaml === null) {
[newText] = AddBlankLineAfterYAML.applyIfEnabled(newText, runOptions.settings, this.disabledRules);
}

const yamlTimestampOptions = YamlTimestamp.getRuleOptions(runOptions.settings);

currentTime = runOptions.getCurrentTime();
Expand All @@ -191,6 +202,7 @@ export class RulesRunner {
yamlTimestampDateModifiedEnabled: isYamlTimestampEnabled && yamlTimestampOptions.dateModified,
dateModifiedKey: yamlTimestampOptions.dateModifiedKey,
});

timingEnd(postRuleLogText);
timingEnd(getTextInLanguage('logs.rule-running'));
return newText;
Expand Down
2 changes: 2 additions & 0 deletions src/rules/add-blank-line-after-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default class AddBlankLineAfterYAML extends RuleBuilder<AddBlankLineAfter
nameKey: 'rules.add-blank-line-after-yaml.name',
descriptionKey: 'rules.add-blank-line-after-yaml.description',
type: RuleType.YAML,
// needs to run before YAML timestamp if the YAML is present, but after it otherwise
hasSpecialExecutionOrder: true,
});
}
get OptionsClass(): new () => AddBlankLineAfterYAMLOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
created: {{created_date}}
last_modified: {{modified_date}}
---

Content here.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Content here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
last_modified: {{modified_date}}
---

Content here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
last_modified: 2024-10-11
---
Content here.
Loading