Skip to content

Commit

Permalink
Merge pull request #16 from marph91/custom-author-date
Browse files Browse the repository at this point in the history
Custom author and date
  • Loading branch information
marph91 authored Aug 7, 2022
2 parents 1e1c6e1 + de9a6c3 commit dc8a5ab
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
69 changes: 69 additions & 0 deletions __tests__/test_background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ beforeEach(() => {
joplinShowNotifications: "onFailure",

joplinSubjectTrimRegex: "",
joplinAuthorTrimRegex: "",
joplinDateFormat: "",
joplinNoteTitleTemplate: "{{subject}} from {{author}}",
joplinNoteHeaderTemplate: "",
joplinNoteParentFolder: "arbitrary folder",
Expand Down Expand Up @@ -521,6 +523,73 @@ describe("process mail", () => {
});
}
);

test.each`
inputAuthor | regexString | expectedAuthor
${"John Doof <[email protected]>"} | ${""} | ${"John Doof <[email protected]>"}
${"John Doof <[email protected]>"} | ${" ?<.*>$"} | ${"John Doof"}
`(
"modify author by regex: $regexString",
async ({ inputAuthor, regexString, expectedAuthor }) => {
const subject = "some subject";
await browser.storage.local.set({ joplinAuthorTrimRegex: regexString });

const result = await processMail({
id: 1,
subject: subject,
author: inputAuthor,
});

expect(result).toBe(null);
expect(requests.length).toBe(1);
expect(requests[0].body).toEqual(
expect.objectContaining({ title: `${subject} from ${expectedAuthor}` })
);

expectConsole({
log: 1,
warn: 0,
error: 0,
});
}
);

test.each`
inputDate | dateFormat | expectedDate
${"06.04.2022"} | ${""} | ${"06.04.2022"}
${new Date("1995-12-17T03:24:00")} | ${"d.L.y T"} | ${"17.12.1995 03:24"}
`(
"apply date format: $dateFormat",
async ({ inputDate, dateFormat, expectedDate }) => {
const author = "author name";
const subject = "some subject";
await browser.storage.local.set({
joplinNoteTitleTemplate: "{{subject}} from {{author}} at {{date}}",
joplinDateFormat: dateFormat,
});

const result = await processMail({
id: 1,
subject: subject,
author: author,
date: inputDate,
});

expect(result).toBe(null);
expect(requests.length).toBe(1);
expect(requests[0].body).toEqual(
expect.objectContaining({
title: `${subject} from ${author} at ${expectedDate}`,
})
);

expectConsole({
log: 1,
warn: 0,
error: 0,
});
}
);
});

describe("process tag", () => {
Expand Down
2 changes: 2 additions & 0 deletions __tests__/test_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const setting_ids = [
"joplinToken",
"joplinShowNotifications",
"joplinSubjectTrimRegex",
"joplinAuthorTrimRegex",
"joplinDateFormat",
"joplinNoteTitleTemplate",
"joplinNoteHeaderTemplate",
"joplinNoteFormat",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"dependencies": {
"browserify": "^17.0.0",
"luxon": "^3.0.1",
"tsify": "^5.0.4"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/jest": "^27.4.1",
"@types/jsdom": "^16.2.14",
"@types/luxon": "^3.0.0",
"@types/node-fetch": "^2.6.1",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
Expand Down
28 changes: 23 additions & 5 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DateTime } from "luxon";
import { generateUrl, getSetting } from "./common";

declare const browser: any;
Expand Down Expand Up @@ -120,13 +121,30 @@ async function processMail(mailHeader: any) {
}

// Add a note with the email content
// Title
const regexString = (await getSetting("joplinSubjectTrimRegex")) || "";
// Customization
const regexStringSubject = (await getSetting("joplinSubjectTrimRegex")) || "";
const regexStringAuthor = (await getSetting("joplinAuthorTrimRegex")) || "";
const dateFormat = (await getSetting("joplinDateFormat")) || "";
const trimmedSubject =
regexString === ""
regexStringSubject === ""
? mailHeader.subject
: mailHeader.subject.replace(new RegExp(regexString), "");
const renderingContext = { ...mailHeader, subject: trimmedSubject };
: mailHeader.subject.replace(new RegExp(regexStringSubject), "");
const trimmedAuthor =
regexStringAuthor === ""
? mailHeader.author
: mailHeader.author.replace(new RegExp(regexStringAuthor), "");
const formattedDate =
dateFormat === ""
? mailHeader.date
: DateTime.fromJSDate(mailHeader.date).toFormat(dateFormat);
const renderingContext = {
...mailHeader,
subject: trimmedSubject,
author: trimmedAuthor,
date: formattedDate,
};

// Title
const titleRendered = renderString(
(await getSetting("joplinNoteTitleTemplate")) || "",
renderingContext
Expand Down
15 changes: 15 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,21 @@
<small>&#9432;</small>
</span>
</p>
<p>
<label for="joplinAuthorTrimRegex">Trim author:</label>
<input id="joplinAuthorTrimRegex" />
<span class="hovertext"
data-hover="Regex for trimming the author string. Useful for removing the email at the end.">
<small>&#9432;</small>
</span>
</p>
<p>
<label for="joplinDateFormat">Date format:</label>
<input id="joplinDateFormat" />
<span class="hovertext" data-hover="Date format. Tokens are listed at the Luxon documentation.">
<small>&#9432;</small>
</span>
</p>
<p>
<label for="joplinNoteTitleTemplate">Note title:</label>
<input id="joplinNoteTitleTemplate" />
Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const default_map: { [key: string]: string | number | boolean } = {
joplinToken: "",
joplinShowNotifications: "onFailure",
joplinSubjectTrimRegex: "",
joplinAuthorTrimRegex: "",
joplinDateFormat: "D T",
joplinNoteTitleTemplate: "{{subject}} from {{author}}",
joplinNoteHeaderTemplate: "",
joplinNoteFormat: "text/html",
Expand Down

0 comments on commit dc8a5ab

Please sign in to comment.