Skip to content

Commit

Permalink
Feat: configurable output format
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko authored Jun 8, 2020
2 parents a46dff8 + ba7d3fa commit 61d976a
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ Those markdown notes that contains external resources such pictures or files, ar
- ```--skip-creation-time```, does not include creation time into metadata section
- ```--skip-update-time```, does not include update time into metadata section
- ```--skip-tags``` , does not include tags into metadata section
- ```--outputFormat```, generates internal file links and highlights in Obsidian-style: highlights are going to be bounded by `==` instead of \` characters, file links are going to be as follows: `![[file-name]]` instead of `![file-name](file-name)`. Possible values: `ObsidianMD` to get Obsidian-style notes, `StandardMD` or skip it completely, if you prefere Standard Markdown format.


### Using cmd:
```shell
npm run start -- --enexSource=GeneralNotes.enex --outputDir=./out --include-metadata --zettelkasten --plaintext-notes-only
npm run start -- --enexSource=GeneralNotes.enex --outputDir=./out --include-metadata --zettelkasten --plaintext-notes-only --outputFormat=ObsidianMD
```

### In program:
Expand All @@ -60,6 +61,10 @@ Those markdown notes that contains external resources such pictures or files, ar

## Release notes

### Version 2.8.0

- New command-line argument introduced : `--outputFormat`. Its optional, one possible value is `ObsidianMD` that configures Yarle to generate internal file links and highlights in Obsidian-style.

### Version 2.7.0

- Huge performance improvement, works with enex files that contain 2k+ notes
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yarle-evernote-to-md",
"version": "2.7.1",
"version": "2.8.0",
"description": "Yet Another Rope Ladder from Evernote",
"keywords": [
"evernote",
Expand Down
3 changes: 3 additions & 0 deletions src/YarleOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { OutputFormat } from './output-format';

export interface YarleOptions {
enexFile?: string;
outputDir?: string;
Expand All @@ -8,4 +10,5 @@ export interface YarleOptions {
skipCreationTime?: boolean;
skipUpdateTime?: boolean;
skipTags?: boolean;
outputFormat?: OutputFormat;
}
2 changes: 2 additions & 0 deletions src/dropTheRope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs';

import * as yarle from './yarle';
import { YarleOptions } from './YarleOptions';
import { OutputFormat } from './output-format';

export const run = async () => {
const argv = minimist(process.argv.slice(2));
Expand All @@ -18,6 +19,7 @@ export const run = async () => {
skipCreationTime: argv['skip-creation-time'] || false ,
skipUpdateTime: argv['skip-update-time'] || false ,
skipTags: argv['skip-tags'] || false ,
outputFormat: argv['outputFormat'] || OutputFormat.StandardMD,
};
if (options.enexFile.endsWith('.enex')) {
console.log(`Converting notes in file: ${options.enexFile}`);
Expand Down
4 changes: 4 additions & 0 deletions src/output-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum OutputFormat {
ObsidianMD= 'ObsidianMD',
StandardMD= 'StandardMD',
}
19 changes: 19 additions & 0 deletions src/utils/turndown-rules/images-rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { yarleOptions } from '../../yarle';

import { filterByNodeName } from './filter-by-nodename';
import { getAttributeProxy } from './get-attribute-proxy';
import { OutputFormat } from './../../output-format';

export const imagesRule = {
filter: filterByNodeName('IMG'),
replacement: (content: any, node: any) => {
const nodeProxy = getAttributeProxy(node);

if (yarleOptions.outputFormat === OutputFormat.ObsidianMD){
return `![[${nodeProxy.src.value}]]`;
}
const srcSpl = nodeProxy.src.value.split('/');

return `![${srcSpl[srcSpl.length - 1]}](${nodeProxy.src.value})`;
},
};
8 changes: 5 additions & 3 deletions src/utils/turndown-rules/span.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@

import { Rule } from 'turndown';

import { yarleOptions } from './../../yarle';
import { filterByNodeName } from './filter-by-nodename';
import { getAttributeProxy } from './get-attribute-proxy';
import { OutputFormat } from './../../output-format';

const EVERNOTE_HIGHLIGHT = '-evernote-highlight:true;';

export const spanRule = {
filter: filterByNodeName('SPAN'),
replacement: (content: any, node: any) => {
const HIGHLIGHT_SEPARATOR = yarleOptions.outputFormat === OutputFormat.ObsidianMD ? '==' : '`' ;
const nodeProxy = getAttributeProxy(node);
if (nodeProxy.style) {
const nodeValue: string = nodeProxy.style.value;

return nodeValue.endsWith(EVERNOTE_HIGHLIGHT) ?
`\`${content}\`` :
`${HIGHLIGHT_SEPARATOR}${content}${HIGHLIGHT_SEPARATOR}` :
content;
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/utils/turndown-rules/wikistyle-links-rule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Rule } from 'turndown';

import { OutputFormat } from './../../output-format';
import { yarleOptions } from './../../yarle';
import { filterByNodeName } from './filter-by-nodename';
import { getAttributeProxy } from './get-attribute-proxy';

Expand All @@ -12,6 +14,8 @@ export const wikiStyleLinksRule = {
(!nodeProxy.href.value.startsWith('http') && !nodeProxy.href.value.startsWith('www')) ||
nodeProxy.href.value.startsWith('evernote://')) ?
`[[${node.innerHTML}]]` :
(yarleOptions.outputFormat === OutputFormat.ObsidianMD) ?
`![[${node.innerHTML}]]` :
`[${node.innerHTML}](${nodeProxy.href.value})`;
}
},
Expand Down
2 changes: 2 additions & 0 deletions src/utils/turndown-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { gfm } from 'joplin-turndown-plugin-gfm';
import { wikiStyleLinksRule } from './turndown-rules/wikistyle-links-rule';
import { taskItemsRule } from './turndown-rules/task-items-rule';
import { spanRule } from './turndown-rules/span';
import { imagesRule } from './turndown-rules/images-rule';

export const getTurndownService = () => {
/* istanbul ignore next */
Expand All @@ -22,6 +23,7 @@ export const getTurndownService = () => {

turndownService.addRule('evernote task items', taskItemsRule);
turndownService.addRule('wikistyle links', wikiStyleLinksRule);
turndownService.addRule('images', imagesRule);
turndownService.addRule('span', spanRule);
turndownService.use(gfm);

Expand Down
8 changes: 8 additions & 0 deletions test/data/test-highlightsObsidian.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Highlights
Let's ==highlight a text==. This is a normal text again.

Created at: 2020-05-29T20:42:21+02:00
Updated at: 2020-05-29T20:42:38+02:00
Where: 18.13938856926725,46.37554931640625


8 changes: 8 additions & 0 deletions test/data/test-obsidianLink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# TEST - note with picture
Squirrels 
![[./_resources/test_-_note_with_picture.resources/pic.jpeg]]

Created at: 2018-10-06T10:44:14+02:00
Updated at: 2018-10-06T11:12:11+02:00


38 changes: 38 additions & 0 deletions test/yarle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as assert from 'assert';
import * as fs from 'fs';

import { OutputFormat } from '../src/output-format';

import * as utils from './../src/utils';
import * as yarle from './../src/yarle';
import { YarleOptions } from './../src/YarleOptions';
Expand Down Expand Up @@ -321,7 +323,22 @@ describe('dropTheRope ', async () => {
fs.readFileSync(`${__dirname}/data/test-highlights.md`, 'utf8'));

});
it('Enex file with highlighted text - Obsidian-style', async () => {
const options: YarleOptions = {
enexFile: './test/data/test-highlights.enex',
outputDir: 'out',
isMetadataNeeded: true,
plainTextNotesOnly: false,
outputFormat: OutputFormat.ObsidianMD,

};
await yarle.dropTheRope(options);
assert.equal(fs.existsSync(`${__dirname}/../out/simpleNotes/test-highlights/highlights.md`), true);

assert.equal(fs.readFileSync(`${__dirname}/../out/simpleNotes/test-highlights/highlights.md`, 'utf8'),
fs.readFileSync(`${__dirname}/data/test-highlightsObsidian.md`, 'utf8'));

});
it('Enex file with PDF attachment', async () => {
const options: YarleOptions = {
enexFile: './test/data/test-pdfAttachment.enex',
Expand All @@ -338,4 +355,25 @@ describe('dropTheRope ', async () => {

});

it('Enex file obsidian style', async () => {
const options: YarleOptions = {
enexFile: './test/data/test-twoNotes.enex',
outputDir: 'out',
isMetadataNeeded: true,
outputFormat: OutputFormat.ObsidianMD,

};
await yarle.dropTheRope(options);
assert.equal(fs.existsSync(`${__dirname}/../out/complexNotes/test-twoNotes/test - note with picture.md`), true);
assert.equal(fs.existsSync(`${__dirname}/../out/complexNotes/test-twoNotes/_resources/test_-_note_with_picture.resources`), true);

assert.equal(fs.readFileSync(`${__dirname}/../out/complexNotes/test-twoNotes/test - note with picture.md`, 'utf8'),
fs.readFileSync(`${__dirname}/data/test-obsidianLink.md`, 'utf8'));
assert.equal(fs.existsSync(`${__dirname}/../out/simpleNotes/test-twoNotes/test -note with text only.md`), true);

assert.equal(fs.readFileSync(`${__dirname}/../out/simpleNotes/test-twoNotes/test -note with text only.md`, 'utf8'),
fs.readFileSync(`${__dirname}/data/test-twoNotes-text.md`, 'utf8'));

});

});

0 comments on commit 61d976a

Please sign in to comment.