Skip to content

Commit

Permalink
feat: adding tags to global regexp (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko authored Sep 9, 2024
1 parent 795d900 commit c8d63f5
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ The following configurational properties are available:
| ```sanitizeResourceNameSpaces``` | true or false | Replace spaces in resource names with the `replacementChar`. e.g "linked file.jpg" will be converted to "linked_file.jpg"
| ```replacementChar``` | string | the replacement character. e.g "linked*file.jpg" will be converted to "linked_file.jpg". It defaults to "_"
| ```replacementCharacterMap``` | JSON Object | it contains character key-value pairs, the keys will be replaced by the proper values in the filename
| ```globalReplacementSettings``` | JSON Array | it contains search and replace pairs, the keys will be replaced by the proper values according to the given type (title or content). Please note: this property has to be in valid JSON Array format, and each item has to have <b>type</b> ('title' or 'content'), <b>regex</b> (what you want to replace as a valid regular expression) and <b>replace</b> (what you want to push instead of the matched text) properties)
| ```globalReplacementSettings``` | JSON Array | it contains search and replace pairs, the keys will be replaced by the proper values according to the given type (title or content). Please note: this property has to be in valid JSON Array format, and each item has to have <b>type</b> ('title', 'content' or 'tag'), <b>regex</b> (what you want to replace as a valid regular expression) and <b>replace</b> (what you want to push instead of the matched text) properties)
| ```keepOriginalAmountOfNewlines``` | true or false | keep the original amount of newlines, default is false, when the multiple newlines are collapsed to one.
```generateNakedUrls``` | true or false | if it's true, Yarle generates 'naked' external Urls without any extra characters. If its false, external Urls are wrapped by '<' and '>' characters
| ```addExtensionToInternalLinks``` | true or false | adds '.md' extensions at the end of internal file links, to make them recognizable by DevonThink and other tools
Expand Down
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
"type": "content",
"regex": "",
"replace": ""
},
{
"type": "tag",
"regex": "",
"replace": ""
}
],
"logseqSettings": {
Expand Down
5 changes: 5 additions & 0 deletions config.logseq.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
"type": "content",
"regex": "",
"replace": ""
},
{
"type": "tag",
"regex": "",
"replace": ""
}
],
"pathSeparator": "/",
Expand Down
5 changes: 5 additions & 0 deletions config.tana.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
"type": "content",
"regex": "",
"replace": ""
},
{
"type": "tag",
"regex": "",
"replace": ""
}
],
"generateNakedUrls": true,
Expand Down
3 changes: 2 additions & 1 deletion src/models/ReplaceType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum ReplaceType {
title = 'title',
content = 'content'
content = 'content',
tag = 'tag'
}
4 changes: 2 additions & 2 deletions src/ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,9 @@ <h5 class="info-text">Attachments</h5>
</div>
<div class="form-group">
<label for="globalReplacementSettings">
Global search and replace (regexp) in note title and content
Global search and replace (regexp) in note title, content and tags
<br><div style='font-size: 12px;'><i>(
Please provide settings in valid JSON Array format, each item has to have <b>type</b> ('title' or 'content'), <b>regex</b> (what you want to replace as a valid regular expression) and <b>replace</b> (what you want to push instead of the matched text) properties)</i></div>
Please provide settings in valid JSON Array format, each item has to have <b>type</b> ('title', 'content' or 'tag'), <b>regex</b> (what you want to replace as a valid regular expression) and <b>replace</b> (what you want to push instead of the matched text) properties)</i></div>
</label>
<textarea class="form-control configurationItem" name="globalReplacementSettings" id="globalReplacementSettings" rows="15"></textarea>

Expand Down
2 changes: 1 addition & 1 deletion src/ui/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const updateDomAndConfig = (configItem, disable) => {
if (domItem.getAttribute('type') === 'checkbox'){
document.getElementById(configItem.name).checked = configItem.value;
} else {
if (isObject(itemValueToStore)){
if (isObject(itemValueToStore) || Array.isArray(itemValueToStore)){
itemValueToStore = JSON.stringify(configItem.value)
document.getElementById(configItem.name).value = JSON.stringify(configItem.value, undefined, 2);;
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/content-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { EvernoteNoteData, NoteData } from './../models';
import { getHtmlFileLink } from './folder-utils';
import { escapeStringRegexp } from './escape-string-regexp';
import { OutputFormat } from './../output-format';
import { performRegexpOnTag } from './get-title';

export const getMetadata = (note: EvernoteNoteData, notebookName: string): MetaData => {

Expand Down Expand Up @@ -97,10 +98,13 @@ export const logTags = (note: EvernoteNoteData): string => {
let cleanTag = tag
.toString()
.replace(/^#/, '');

cleanTag = performRegexpOnTag(yarleOptions, cleanTag)
if (tagOptions) {
cleanTag = cleanTag.replace(new RegExp(escapeStringRegexp(tagOptions.separatorInEN), 'g'), tagOptions.replaceSeparatorWith);
}


const replaceSpaceWith = (tagOptions && tagOptions.replaceSpaceWith) || '-';

cleanTag = cleanTag.replace(/ /g, replaceSpaceWith);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/get-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ export const performRegexpOnTitle = (options: YarleOptions, title: string): stri

export const performRegexpOnContent = (options: YarleOptions, content: string): string => {
return regexpProcess(options, content, ReplaceType.content);
}

export const performRegexpOnTag = (options: YarleOptions, tag: string): string => {
return regexpProcess(options, tag, ReplaceType.tag)
}
5 changes: 5 additions & 0 deletions test/data/test-noteWithNestedTagsWithGlobalRegex.enex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
<en-export export-date="20181006T112423Z" application="Evernote" version="Evernote Mac 7.5 (457109)">
<note><title>test -note with text only</title><content><![CDATA[<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div>This is the content</div></en-note>]]></content><created>20181006T084349Z</created><updated>20181006T084411Z</updated><tag>tag1_nestedTag1</tag><tag>tag2_nestedTag2</tag><note-attributes><author>akos</author><source>desktop.mac</source><reminder-order>0</reminder-order></note-attributes></note>
</en-export>
12 changes: 12 additions & 0 deletions test/data/test-noteWithNestedTagsWithGlobalRegex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# test -note with text only

---
Tag(s): #tag1/nestedTag1 #tag2/nestedTag2

---

This is the content

Created at: 2018-10-06T09:43:49+01:00
Updated at: 2018-10-06T09:44:11+01:00

19 changes: 19 additions & 0 deletions test/yarle-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { YarleTest } from './yarle-test';
import { OutputFormat } from "../src/output-format";
import * as path from 'path';
import { YarleTestModifierOptions } from './yarle-test-modifier-options';
import { ReplaceType } from '../src/models';

const dataFolder = `${path.sep}data${path.sep}`;
const testDataFolder = `${path.sep}test${dataFolder}`;
Expand Down Expand Up @@ -95,6 +96,24 @@ export const yarleTests: Array<YarleTest> = [

expectedOutputPath: `${dataFolder}test-noteWithNestedTags.md`,
},
{
name: 'Note with nested tags with global regexp',
options: {
enexSources: [ `.${testDataFolder}test-noteWithNestedTagsWithGlobalRegex.enex` ],
outputDir: 'out',
isMetadataNeeded: true,
globalReplacementSettings: [
{
type: ReplaceType.tag,
regex: "_+",
replace: "/"
}
],
useHashTags: true
},
testOutputPath: `notes${path.sep}test-noteWithNestedTagsWithGlobalRegex${path.sep}test -note with text only.md`,
expectedOutputPath: `${dataFolder}test-noteWithNestedTagsWithGlobalRegex.md`,
},
{
name: 'Note with nested tags containing spaces',
options: {
Expand Down

0 comments on commit c8d63f5

Please sign in to comment.