Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
platers committed Jan 11, 2022
2 parents 3b3681c + 0b7d4dc commit 68f8708
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
29 changes: 25 additions & 4 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,45 @@ After:

```markdown
---
tags: one, two, three, nested/four/five
tags: one two three nested/four/five
---
```
Example: Format Tags in YAML frontmatter
Example: Format tags in array

Before:

```markdown
---
tags: [#one #two #three]
---
```

After:

```markdown
---
tags: [one two three]
---
```
Example: Format tags in list

Before:

```markdown
---
tags: #one, #two, #three
tags:
- #tag1
- #tag2
---
```

After:

```markdown
---
tags: one, two, three
tags:
- tag1
- tag2
---
```

Expand Down
32 changes: 25 additions & 7 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,8 @@ export const rules: Rule[] = [
RuleType.YAML,
(text: string) => {
return formatYAML(text, (text) => {
return text.replace(/\ntags: [\w\u2E80-\u9FFF#/ ,-]+(?=.*\n---)/is, function(tagsYAML) {
return tagsYAML.replaceAll('#', '').replaceAll(' ', ', ').replaceAll(',,', ',').replace('tags:,', 'tags:');
return text.replace(/\ntags:(.*?)(?=\n(?:---|\w+:))/s, function(tagsYAML) {
return tagsYAML.replaceAll('#', '');
});
});
},
Expand All @@ -573,23 +573,40 @@ export const rules: Rule[] = [
`,
dedent`
---
tags: one, two, three, nested/four/five
tags: one two three nested/four/five
---
`,
),
new Example(
'Format Tags in YAML frontmatter',
'Format tags in array',
dedent`
---
tags: #one, #two, #three
tags: [#one #two #three]
---
`,
dedent`
---
tags: one, two, three
tags: [one two three]
---
`,
),
new Example(
'Format tags in list',
dedent`
---
tags:
- #tag1
- #tag2
---
`,
dedent`
---
tags:
- tag1
- tag2
---
`,
),
],
),
new Rule(
Expand Down Expand Up @@ -897,7 +914,8 @@ export const rules: Rule[] = [
lines[i] = lines[i].toUpperCase(); // convert full heading to uppercase
break;
case 'First Letter':
lines[i] = lines[i].replace(/^#*\s([a-z])/, (string) => string.toUpperCase()); // capitalize first letter of heading
lines[i] = lines[i].toLowerCase()
.replace(/^#*\s([a-z])/, (string) => string.toUpperCase()); // capitalize first letter of heading
break;
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ describe('Rules tests', () => {
`;
expect(rulesDict['capitalize-headings'].apply(before, {'Style': 'Title Case'})).toBe(after);
});
it('Can capitalize only first letter', () => {
const before = dedent`
# this Is A Heading
`;
const after = dedent`
# This is a heading
`;
expect(rulesDict['capitalize-headings'].apply(before, {'Style': 'First Letter'})).toBe(after);
});
it('Can capitalize to all caps', () => {
const before = dedent`
# this Is A Heading
`;
const after = dedent`
# THIS IS A HEADING
`;
expect(rulesDict['capitalize-headings'].apply(before, {'Style': 'All Caps'})).toBe(after);
});
});
describe('File Name Heading', () => {
it('Handles stray dashes', () => {
Expand Down

0 comments on commit 68f8708

Please sign in to comment.