Skip to content

Commit

Permalink
feat: evernote guid és evernote link in templates implemented (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko authored Jan 15, 2024
1 parent 438da48 commit 49088b7
Show file tree
Hide file tree
Showing 21 changed files with 274 additions and 5 deletions.
13 changes: 13 additions & 0 deletions Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ A custom template file can have any file name and extension.
| Link to the HTML file | `{link-to-original-block}{link-to-original}{end-link-to-original-block}` | Generate an HTML version of the note in the note's resource folder. A link to the HTML file is included here. |
| Location | `{location-block}{location}{end-location-block}` | The GPS coordinates of the location from the EN location details. |

## Extra template blocks if there is TOC

If a Table of contents is generated in the enex, feel free to add these blocks to the template:

Table of contents can be created in EN v10+ if you select all of your notes, then right click and choose `Copy internal links` then `Copy app links` . Then create a new note, title MUST BE: Table of Contents, and paste the internal links into the note's body. That's all.

| Name | Block and content tags | Description |
|-|-|-|
| Evernote link |  `{evernotelink-block}{evernotelink}{end-evernotelink-block}`| Link to the original note in Evernote |
| Evernote guid |  `{evernoteguid-block}{evernoteguid}{end-evernoteguid-block}`| Guid of the original note in Evernote |



## Example template:

This example puts the note title at the top, preceded by `#`. The tags are listed next, surrounded by horizontal rules. The note content follows. Finally, the note's metadata is included, indented, at the bottom of the note. The output also includes an HTML version of the note, with a link to it in the Markdown file.
Expand Down
5 changes: 3 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export const tanaCodeBlock ='<YARLE_TANA_CODE_BLOCK>';
export const checkboxTodo = '- [ ]'
export const checkboxDone = '- [x]'
export const tanaTableBlock = '<YARLE_TANA_TABLE>';
export const tanaTableRowBlock = '<YARLE_TANA_TABLE_ROW>'
export const tanaTableColBlock = '<YARLE_TANA_TABLE_COL>'
export const tanaTableRowBlock = '<YARLE_TANA_TABLE_ROW>';
export const tanaTableColBlock = '<YARLE_TANA_TABLE_COL>';
export const TOCNoteName = 'Table of Contents';
15 changes: 14 additions & 1 deletion src/runtime-properties.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { cloneDeep } from 'lodash';
import { InternalLink, NoteData } from './models';
export interface NoteIdNameEntry {
title: string;
noteName: string;
notebookName: string;
uniqueEnd: string;
url: string
url: string,
guid: string,
}

export interface NoteIdNames {
[key: string]: NoteIdNameEntry;
}

const getGuid = (linkItem: InternalLink): string => {
const linkSpl = cloneDeep(linkItem.url).split('/').reverse().filter(item => !!item)
return (linkSpl.length > 1)
? linkSpl[1]
: linkSpl[0]
}
export class RuntimePropertiesSingleton {

static instance: RuntimePropertiesSingleton;
Expand All @@ -35,9 +44,11 @@ export class RuntimePropertiesSingleton {
}

addItemToMap(linkItem: InternalLink): void {

this.noteIdNameMap[linkItem.id] = {
...this.noteIdNameMap[linkItem.id],
url: linkItem.url,
guid: getGuid(linkItem),
title: linkItem.title,
noteName: this.currentNoteName,
notebookName: this.currentNotebookName,
Expand All @@ -46,10 +57,12 @@ export class RuntimePropertiesSingleton {
};
}
addItemToTOCMap(linkItem: InternalLink): void {

this.noteIdNameTOCMap[linkItem.id] = {
...this.noteIdNameMap[linkItem.id],
title: linkItem.title,
url: linkItem.url,
guid: getGuid(linkItem),
noteName: this.currentNoteName,
notebookName: this.currentNotebookName,
uniqueEnd: linkItem.uniqueEnd,
Expand Down
14 changes: 13 additions & 1 deletion src/utils/apply-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getAllOutputFilesWithExtension } from './get-all-output-files';
import { isTanaOutput } from './tana/is-tana-output';
import { updateFileContentSafely } from './file-utils';
import { getClosestFileName } from './filename-utils';
import { TOCNoteName } from './../constants'

export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<string>): void => {
const linkNameMap = RuntimePropertiesSingleton.getInstance();
Expand Down Expand Up @@ -66,8 +67,19 @@ export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<s
}
const regexp = new RegExp(escapeStringRegexp(linkName), 'g');
updatedContent = updatedContent.replace(regexp, realFileNameInContent);


if ((`${fileName}.md` === targetFile || targetFile === linkProps.title) &&
linkProps.noteName === TOCNoteName &&
notebookFolder.endsWith(notebookName)){
// TODO APPLY EVERNOTE LINK
const evernoteInternalLinkPlaceholderRegExp = new RegExp('<YARLE_EVERNOTE_LINK_PLACEHOLDER>', 'g');
updatedContent = updatedContent.replace(evernoteInternalLinkPlaceholderRegExp, (linkProps as any)['url']);


// TODO APPLY EVERNOTE GUID
const evernoteGuidPlaceholderRegExp = new RegExp('<YARLE_EVERNOTE_GUID_PLACEHOLDER>', 'g');
updatedContent = updatedContent.replace(evernoteGuidPlaceholderRegExp, (linkProps as any)['guid']);
}
if (fileContent !== updatedContent) {
const filePath = `${notebookFolder}${path.sep}${targetFile}`;
updateFileContentSafely(filePath, updatedContent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { cloneDeep } from 'lodash';

import { NoteData } from '../../../models/NoteData';
import * as P from './../placeholders/evernoteguid-placeholders';
import { applyConditionalTemplate } from './apply-conditional-template';

export const applyEvernoteGuidTemplate = (noteData: NoteData, text: string): string => {
return applyConditionalTemplate(text, P, "<YARLE_EVERNOTE_GUID_PLACEHOLDER>");
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { cloneDeep } from 'lodash';

import { NoteData } from '../../../models/NoteData';
import * as P from './../placeholders/evernotelink-placeholders';
import { applyConditionalTemplate } from './apply-conditional-template';

export const applyEvernoteLinkTemplate = (noteData: NoteData, text: string): string => {
return applyConditionalTemplate(text, P, "<YARLE_EVERNOTE_LINK_PLACEHOLDER>");
};
2 changes: 2 additions & 0 deletions src/utils/templates/apply-functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export * from './apply-remindertime-template';
export * from './apply-reminderorder-template';
export * from './apply-reminderdonetime-template';
export * from './apply-tags-array-template';
export * from './apply-evernotelink-template';
export * from './apply-evernoteguid-template';
4 changes: 4 additions & 0 deletions src/utils/templates/placeholders/evernoteguid-placeholders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const CONTENT_PLACEHOLDER = '{evernoteguid}';
export const START_BLOCK = '{evernoteguid-block}';
export const END_BLOCK = '{end-evernoteguid-block}';
4 changes: 4 additions & 0 deletions src/utils/templates/placeholders/evernotelink-placeholders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const CONTENT_PLACEHOLDER = '{evernotelink}';
export const START_BLOCK = '{evernotelink-block}';
export const END_BLOCK = '{end-evernotelink-block}';
2 changes: 2 additions & 0 deletions src/utils/templates/remove-functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export * from './remove-link-to-original-placeholder';
export * from './remove-remindertime-placeholder';
export * from './remove-reminderdonetime-placeholder';
export * from './remove-reminderorder-placeholder';
export * from './remove-evernoteguid-placeholder';
export * from './remove-evernotelink-placeholder';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as T from '../placeholders/evernoteguid-placeholders';

import { removePlaceholder } from './remove-placeholder';

export const removeEvernoteGuidPlaceholder = (text: string): string => {
return removePlaceholder(text, T);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as T from '../placeholders/evernotelink-placeholders';

import { removePlaceholder } from './remove-placeholder';

export const removeEvernoteLinkPlaceholder = (text: string): string => {
return removePlaceholder(text, T);
};
13 changes: 12 additions & 1 deletion src/utils/templates/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { cloneDeep } from 'lodash';

import { YarleOptions } from '../../YarleOptions';
import { NoteData } from '../../models/NoteData';

import { TOCNoteName } from './../../constants';
import * as T from './placeholders/metadata-placeholders';
import * as M from './match-all';
import {
Expand All @@ -20,6 +20,8 @@ import {
applyTagsTemplate,
applyTitleTemplate,
applyUpdatedAtTemplate,
applyEvernoteLinkTemplate,
applyEvernoteGuidTemplate
} from './apply-functions';
import {
removeCreatedAtPlaceholder,
Expand All @@ -32,6 +34,8 @@ import {
removeReminderTimePlaceholder,
removeSourceUrlPlaceholder,
removeUpdatedAtPlaceholder,
removeEvernoteLinkPlaceholder,
removeEvernoteGuidPlaceholder
} from './remove-functions';

export const applyTemplate = (noteData: NoteData, yarleOptions: YarleOptions) => {
Expand All @@ -41,6 +45,13 @@ 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 = noteData.title === TOCNoteName
? removeEvernoteLinkPlaceholder(result)
: applyEvernoteLinkTemplate(noteData, result)

result = noteData.title === TOCNoteName
? removeEvernoteGuidPlaceholder(result)
: applyEvernoteGuidTemplate(noteData, result)

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

Expand Down
14 changes: 14 additions & 0 deletions test/data/evernotelink-template.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{title-block}# {title}{end-title-block}

{tags-block}
---
Tag(s): {tags}

---
{end-tags-block}

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


{evernotelink-block} Evernote Link: {evernotelink}{end-evernotelink-block}
{evernoteguid-block} Evernote GUID: {evernoteguid}{end-evernoteguid-block}
48 changes: 48 additions & 0 deletions test/data/test-original-links.enex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">
<en-export export-date="20240115T160506Z" application="Evernote" version="10.70.2">
<note>
<title>Table of Contents</title>
<created>20240115T155959Z</created>
<updated>20240115T160436Z</updated>
<note-attributes>
</note-attributes>
<content>
<![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div><a href="evernote:///view/244421476/s470/27698765-eecc-2897-74d7-81098e4f2f48/d8ce9322-b866-453a-90b3-81923b70c474/">EvernoteNoteA</a></div><div><a href="evernote:///view/244421476/s470/306474c1-33f8-4aa3-2d0c-1b28a116e2b8/d8ce9322-b866-453a-90b3-81923b70c474/" rel="noopener noreferrer" rev="en_rl_none">EvernoteNoteB</a></div><div><a href="evernote:///view/244421476/s470/f77780bb-6823-55f8-e183-b326e4b9200a/d8ce9322-b866-453a-90b3-81923b70c474/" rel="noopener noreferrer" rev="en_rl_none">EvernoteNoteC</a></div></en-note> ]]>
</content>
</note>
<note>
<title>EvernoteNoteC</title>
<created>20240115T155949Z</created>
<updated>20240115T155955Z</updated>
<note-attributes>
</note-attributes>
<content>
<![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div><br/></div></en-note> ]]>
</content>
</note>
<note>
<title>EvernoteNoteB</title>
<created>20240115T155943Z</created>
<updated>20240115T155950Z</updated>
<note-attributes>
</note-attributes>
<content>
<![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div><br/></div></en-note> ]]>
</content>
</note>
<note>
<title>EvernoteNoteA</title>
<created>20240115T155924Z</created>
<updated>20240115T155942Z</updated>
<note-attributes>
</note-attributes>
<content>
<![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div><br/></div></en-note> ]]>
</content>
</note>
</en-export>
9 changes: 9 additions & 0 deletions test/data/test-original-links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Links - NoteB



[[Links - NoteA|Link to NoteA]]


Evernote Link: evernote:///view/16093948/s150/f5885bd2-9bb0-74b9-6f7b-f70bd5afb5d8/d935905b-20a4-4b9e-91b5-cfa8d80ab818
Evernote GUID: d935905b-20a4-4b9e-91b5-cfa8d80ab818
9 changes: 9 additions & 0 deletions test/data/test-originallink-EvernoteNoteA.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# EvernoteNoteA






Evernote Link: evernote:///view/244421476/s470/27698765-eecc-2897-74d7-81098e4f2f48/d8ce9322-b866-453a-90b3-81923b70c474/
Evernote GUID: 27698765-eecc-2897-74d7-81098e4f2f48
9 changes: 9 additions & 0 deletions test/data/test-originallink-EvernoteNoteB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# EvernoteNoteB






Evernote Link: evernote:///view/244421476/s470/306474c1-33f8-4aa3-2d0c-1b28a116e2b8/d8ce9322-b866-453a-90b3-81923b70c474/
Evernote GUID: 306474c1-33f8-4aa3-2d0c-1b28a116e2b8
9 changes: 9 additions & 0 deletions test/data/test-originallink-EvernoteNoteC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# EvernoteNoteC






Evernote Link: evernote:///view/244421476/s470/f77780bb-6823-55f8-e183-b326e4b9200a/d8ce9322-b866-453a-90b3-81923b70c474/
Evernote GUID: f77780bb-6823-55f8-e183-b326e4b9200a
9 changes: 9 additions & 0 deletions test/data/test-originallink-Table of Contents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Table of Contents



[[EvernoteNoteA|EvernoteNoteA]]
[[EvernoteNoteB|EvernoteNoteB]]
[[EvernoteNoteC|EvernoteNoteC]]


Loading

0 comments on commit 49088b7

Please sign in to comment.