Skip to content

Commit

Permalink
feat: array tags (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko authored Oct 30, 2022
1 parent 2980d4a commit a8c7552
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 10 deletions.
26 changes: 23 additions & 3 deletions Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
You can control the Markdown output generated by YARLE by using a custom template file like the one below, which is similar to YARLE's [default template](./src/utils/templates/default-template.ts).

## Calling the template
To use a custom template with YARLE, include the `templateFile` parameter in your `config.json` file. For example,
To use a custom template with YARLE, include the `templateFile` parameter in your `config.json` file. For example,

```json
"templateFile": "/absolute-path-to-your-template-dir/myTemplate.tmpl"
Expand Down Expand Up @@ -31,6 +31,7 @@ A custom template file can have any file name and extension.
|-|-|-|
| Title | `{title-block}{title}{end-title-block}` | Note title |
| Tags | `{tags-block}{tags}{end-tags-block}` | Note tags |
| Array Tags |`{tags-array-block}{tags-array}{end-tags-array-block}` | Note tags in array format (to be compatible with Obsidian's Yaml Frontmatter)
| Content | `{content-block}{content}{end-content-block}` | Note content |
| Date of creation | `{created-at-block}{created-at}{end-created-at-block}` | Creation date of the note |
| Reminder Time | `{reminder-time-block}{reminder-time}{end-reminder-time-block}` | Reminder time of the note |
Expand All @@ -51,7 +52,7 @@ This example puts the note title at the top, preceded by `#`. The tags are liste
{tags-block}
---
Tag(s): {tags}
Tags: {tags}
---
{end-tags-block}
Expand All @@ -62,11 +63,30 @@ Tag(s): {tags}
{updated-at-block} Updated at: {updated-at}{end-updated-at-block}
{source-url-block} Source: {source-url}{end-source-url-block}
{notebook-block} Notebook: {notebook}{end-notebook-block}
{location-block} Where: {location}{end-location-block}
{location-block} Where: {location}{end-location-block}
{link-to-original-block} Link to original content: {link-to-original}{end-link-to-original-block}
```


As a starting point, you can edit the `sampleTemplate.tmpl` default template. Don't forget to specify the `templateFile` in your config file.


## Example Tags array template:
```
{title-block}# {title}{end-title-block}
{tags-array-block}
---
Tags: {tags-array}
---
{end-tags-array-block}
{content-block}{content}{end-content-block}
{created-at-block} Created at: {created-at}{end-created-at-block}
{updated-at-block} Updated at: {updated-at}{end-updated-at-block}
{source-url-block} Source: {source-url}{end-source-url-block}
{notebook-block} Notebook: {notebook}{end-notebook-block}
```
Have fun! 😃
15 changes: 15 additions & 0 deletions sampleTemplate_tags-array.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{title-block}# {title}{end-title-block}

{tags-array-block}
---
Tags: {tags-array}

---
{end-tags-array-block}

{content-block}{content}{end-content-block}

{created-at-block} Created at: {created-at}{end-created-at-block}
{updated-at-block} Updated at: {updated-at}{end-updated-at-block}
{source-url-block} Source: {source-url}{end-source-url-block}
{notebook-block} Notebook: {notebook}{end-notebook-block}
2 changes: 1 addition & 1 deletion src/YarleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ export interface YarleOptions {
turndownOptions?: Record<string, any>;
taskOutputFormat?: TaskOutputFormat;
obsidianTaskTag?: string;
useUniqueUnknownFileNames?: boolean;
useUniqueUnknownFileNames?: boolean;
}
1 change: 0 additions & 1 deletion src/utils/escape-string-regexp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const escapeStringRegexp = (text: string): string =>  {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
};

16 changes: 16 additions & 0 deletions src/utils/templates/apply-functions/apply-tags-array-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { cloneDeep, templateSettings } from 'lodash';

import { NoteData } from '../../../models/NoteData';
import * as P from '../placeholders/tags-array-placeholders';

import { applyTemplateOnBlock } from './apply-template-on-block';
import { getTemplateBlockSettings } from './get-templateblock-settings';
export const applyTagsArrayTemplate = (noteData: NoteData, inputText: string, check: Function): string => {
const result = cloneDeep(inputText);

if (noteData.tags)
noteData.tags = JSON.stringify(noteData.tags.split(' '));
const tagsTemplateSettings = getTemplateBlockSettings(result, check, P, noteData.tags);

return applyTemplateOnBlock(tagsTemplateSettings);
};
1 change: 1 addition & 0 deletions src/utils/templates/apply-functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './apply-notebook-template';
export * from './apply-remindertime-template';
export * from './apply-reminderorder-template';
export * from './apply-reminderdonetime-template';
export * from './apply-tags-array-template';
7 changes: 5 additions & 2 deletions src/utils/templates/checker-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as NOTEBOOK from './placeholders/notebook-placeholders';
import * as ORIGINALLINK from './placeholders/original-placeholders';
import * as SOURCEURL from './placeholders/sourceurl-placeholders';
import * as TAGS from './placeholders/tags-placeholders';
import * as YAMLARRAYTAGS from './placeholders/tags-array-placeholders';
import * as METADATA from './placeholders/metadata-placeholders';
import * as UPDATETIME from './placeholders/updatedat-placeholders';
import * as REMINDERTIME from './placeholders/remindertime-placeholders';
Expand Down Expand Up @@ -38,9 +39,11 @@ export const hasOriginalLinkInTemplate = (templateContent: string): boolean => 
export const hasSourceURLInTemplate = (templateContent: string): boolean => {
return hasItemInTemplate(SOURCEURL, templateContent);
};
export const hasTagsInTemplate = (templateContent: string): boolean => {
return hasItemInTemplate(TAGS, templateContent);
export const hasAnyTagsInTemplate = (templateContent: string): boolean => {
return (hasItemInTemplate(TAGS, templateContent)
|| hasItemInTemplate(YAMLARRAYTAGS, templateContent));
};

export const hasMetadataInTemplate = (templateContent: string): boolean => {
return templateContent.includes(METADATA.START_BLOCK) &&
templateContent.includes(METADATA.END_BLOCK);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/templates/placeholders/tags-array-placeholders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const CONTENT_PLACEHOLDER = '{tags-array}';
export const START_BLOCK = '{tags-array-block}';
export const END_BLOCK = '{end-tags-array-block}';
3 changes: 3 additions & 0 deletions src/utils/templates/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
applyReminderOrderTemplate,
applyReminderTimeTemplate,
applySourceUrlTemplate,
applyTagsArrayTemplate,
applyTagsTemplate,
applyTitleTemplate,
applyUpdatedAtTemplate,
Expand All @@ -39,6 +40,8 @@ export const applyTemplate = (noteData: NoteData, yarleOptions: YarleOptions) =>

result = applyTitleTemplate(noteData, result, () => noteData.title);
result = applyTagsTemplate(noteData, result, () => !yarleOptions.skipTags);
result = applyTagsArrayTemplate(noteData, result, () => !yarleOptions.skipTags);

result = applyContentTemplate(noteData, result, () => noteData.content);

result = (yarleOptions.keepOriginalHtml && noteData.linkToOriginal)
Expand Down
6 changes: 3 additions & 3 deletions src/yarle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { YarleOptions } from './YarleOptions';
import { processNode } from './process-node';
import { isWebClip } from './utils/note-utils';
import { loggerInfo } from './utils/loggerInfo';
import { hasCreationTimeInTemplate,
import { hasAnyTagsInTemplate,
hasCreationTimeInTemplate,
hasLinkToOriginalInTemplate,
hasLocationInTemplate,
hasNotebookInTemplate,
hasSourceURLInTemplate,
hasTagsInTemplate,
hasUpdateTimeInTemplate } from './utils/templates/checker-functions';
import { defaultTemplate } from './utils/templates/default-template';
import { OutputFormat } from './output-format';
Expand Down Expand Up @@ -65,7 +65,7 @@ const setOptions = (options: YarleOptions): void => {
yarleOptions.skipCreationTime = !hasCreationTimeInTemplate(template);
yarleOptions.skipLocation = !hasLocationInTemplate(template);
yarleOptions.skipSourceUrl = !hasSourceURLInTemplate(template);
yarleOptions.skipTags = !hasTagsInTemplate(template);
yarleOptions.skipTags = !hasAnyTagsInTemplate(template);
yarleOptions.skipUpdateTime = !hasUpdateTimeInTemplate(template);
yarleOptions.isNotebookNameNeeded = hasNotebookInTemplate(template);
yarleOptions.keepOriginalHtml = hasLinkToOriginalInTemplate(template);
Expand Down
15 changes: 15 additions & 0 deletions test/data/tags-array_template.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{title-block}# {title}{end-title-block}

{tags-array-block}
---
Tags: {tags-array}

---
{end-tags-array-block}

{content-block}{content}{end-content-block}

{created-at-block} Created at: {created-at}{end-created-at-block}
{updated-at-block} Updated at: {updated-at}{end-updated-at-block}
{source-url-block} Source: {source-url}{end-source-url-block}
{notebook-block} Notebook: {notebook}{end-notebook-block}
14 changes: 14 additions & 0 deletions test/data/test -note with tags array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# test -note with text only


---
Tags: ["tag1","tag2"]

---


This is the content

Created at: 2018-10-06T09:43:49+01:00
Updated at: 2018-10-06T09:44:11+01:00
Notebook: test-noteWithTags
26 changes: 26 additions & 0 deletions test/yarle-special-cases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -947,5 +947,31 @@ describe('Yarle special cases', async () => {
fs.readFileSync(`${__dirname}/data/test-old-note.md`, 'utf8'),
);
});
it('yaml tags list', async () => {
const options: YarleOptions = {
enexSources: [ `${testDataFolder}test-noteWithTags.enex` ],
outputDir: 'out',
templateFile: `${testDataFolder}tags-array_template.templ`,
isMetadataNeeded: true,
outputFormat: OutputFormat.ObsidianMD,
skipEnexFileNameFromOutputPath: false,
skipTags: false,
useHashTags: false,

};
await yarle.dropTheRope(options);
assert.equal(
fs.existsSync(
`${__dirname}/../out/notes/test-noteWithTags/test -note with text only.md`,
),
true,
);
assert.equal(
eol.auto(fs.readFileSync(
`${__dirname}/../out/notes/test-noteWithTags/test -note with text only.md`,
'utf8',
)),
fs.readFileSync(`${__dirname}/data/test -note with tags array.md`, 'utf8'),
);
});
});

0 comments on commit a8c7552

Please sign in to comment.