From a8c7552ce155cf8abecc14643360c67bac2df0c4 Mon Sep 17 00:00:00 2001 From: Akos Balasko Date: Sun, 30 Oct 2022 08:23:59 +0100 Subject: [PATCH] feat: array tags (#415) --- Templates.md | 26 ++++++++++++++++--- sampleTemplate_tags-array.tmpl | 15 +++++++++++ src/YarleOptions.ts | 2 +- src/utils/escape-string-regexp.ts | 1 - .../apply-tags-array-template.ts | 16 ++++++++++++ src/utils/templates/apply-functions/index.ts | 1 + src/utils/templates/checker-functions.ts | 7 +++-- .../placeholders/tags-array-placeholders.ts | 4 +++ src/utils/templates/templates.ts | 3 +++ src/yarle.ts | 6 ++--- test/data/tags-array_template.templ | 15 +++++++++++ test/data/test -note with tags array.md | 14 ++++++++++ test/yarle-special-cases.spec.ts | 26 +++++++++++++++++++ 13 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 sampleTemplate_tags-array.tmpl create mode 100644 src/utils/templates/apply-functions/apply-tags-array-template.ts create mode 100644 src/utils/templates/placeholders/tags-array-placeholders.ts create mode 100644 test/data/tags-array_template.templ create mode 100644 test/data/test -note with tags array.md diff --git a/Templates.md b/Templates.md index ffac7782..7821afcc 100644 --- a/Templates.md +++ b/Templates.md @@ -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" @@ -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 | @@ -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} @@ -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! 😃 diff --git a/sampleTemplate_tags-array.tmpl b/sampleTemplate_tags-array.tmpl new file mode 100644 index 00000000..ea08e7fe --- /dev/null +++ b/sampleTemplate_tags-array.tmpl @@ -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} diff --git a/src/YarleOptions.ts b/src/YarleOptions.ts index b4f601cc..8c50be24 100644 --- a/src/YarleOptions.ts +++ b/src/YarleOptions.ts @@ -50,5 +50,5 @@ export interface YarleOptions { turndownOptions?: Record; taskOutputFormat?: TaskOutputFormat; obsidianTaskTag?: string; - useUniqueUnknownFileNames?: boolean; + useUniqueUnknownFileNames?: boolean; } diff --git a/src/utils/escape-string-regexp.ts b/src/utils/escape-string-regexp.ts index 6066fb97..3dc15ba6 100644 --- a/src/utils/escape-string-regexp.ts +++ b/src/utils/escape-string-regexp.ts @@ -1,4 +1,3 @@ export const escapeStringRegexp = (text: string): string =>  { return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); }; - \ No newline at end of file diff --git a/src/utils/templates/apply-functions/apply-tags-array-template.ts b/src/utils/templates/apply-functions/apply-tags-array-template.ts new file mode 100644 index 00000000..4aed9eb9 --- /dev/null +++ b/src/utils/templates/apply-functions/apply-tags-array-template.ts @@ -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); +}; diff --git a/src/utils/templates/apply-functions/index.ts b/src/utils/templates/apply-functions/index.ts index 1b65f407..2804c4ad 100644 --- a/src/utils/templates/apply-functions/index.ts +++ b/src/utils/templates/apply-functions/index.ts @@ -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'; diff --git a/src/utils/templates/checker-functions.ts b/src/utils/templates/checker-functions.ts index 94a02b4b..4d96f1bf 100644 --- a/src/utils/templates/checker-functions.ts +++ b/src/utils/templates/checker-functions.ts @@ -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'; @@ -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); diff --git a/src/utils/templates/placeholders/tags-array-placeholders.ts b/src/utils/templates/placeholders/tags-array-placeholders.ts new file mode 100644 index 00000000..42532729 --- /dev/null +++ b/src/utils/templates/placeholders/tags-array-placeholders.ts @@ -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}'; diff --git a/src/utils/templates/templates.ts b/src/utils/templates/templates.ts index ec9f9d8c..28a7a3ec 100644 --- a/src/utils/templates/templates.ts +++ b/src/utils/templates/templates.ts @@ -16,6 +16,7 @@ import { applyReminderOrderTemplate, applyReminderTimeTemplate, applySourceUrlTemplate, + applyTagsArrayTemplate, applyTagsTemplate, applyTitleTemplate, applyUpdatedAtTemplate, @@ -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) diff --git a/src/yarle.ts b/src/yarle.ts index 2c46d7b6..13699b3c 100644 --- a/src/yarle.ts +++ b/src/yarle.ts @@ -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'; @@ -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); diff --git a/test/data/tags-array_template.templ b/test/data/tags-array_template.templ new file mode 100644 index 00000000..ea08e7fe --- /dev/null +++ b/test/data/tags-array_template.templ @@ -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} diff --git a/test/data/test -note with tags array.md b/test/data/test -note with tags array.md new file mode 100644 index 00000000..0888d5e4 --- /dev/null +++ b/test/data/test -note with tags array.md @@ -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 diff --git a/test/yarle-special-cases.spec.ts b/test/yarle-special-cases.spec.ts index 46e45e65..f7ef879e 100644 --- a/test/yarle-special-cases.spec.ts +++ b/test/yarle-special-cases.spec.ts @@ -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'), + ); + }); });