Skip to content

Commit

Permalink
Add placeholder variables (version 0.3.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
joleaf committed Jan 4, 2023
1 parent aa497f8 commit 24b2902
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

All changes to this plugin are listed here.

## 0.2.0 (30.12.2022)
## 0.3.0 (2023-01-04)

### New

- Add placeholder variables

## 0.2.0 (2022-12-30)

### New

- A note can be used as body content using an internal link.

## 0.1.1 (28.12.2022)
## 0.1.1 (2022-12-28)

### Changed

- Use obsidian parseYaml

## 0.1.0 (17.12.2022)
## 0.1.0 (2022-12-17)

### Added

Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This plugin lets you plan small emails inside your [Obsidian](https://www.obsidi
Add the "email" code block into your note:

... with plain text as body content:

````
```email
to: [email protected]
Expand All @@ -32,14 +33,21 @@ body: "Hey info,
````

... with a referenced note as body content:

````
```email
to: [email protected]
subject: My Subject
body: [[MyMail4711]]
variables:
myvar: TestVar
```
````

You can use the `variables` parameter to replace placeholders in your body text with the variable values.
To include a variable in the body text just add a placeholder `{{myvar}}`.
Variables from frontmatter data can be used as well.

### Parameter

You can customize the view with the following parameters:
Expand All @@ -52,13 +60,17 @@ You can customize the view with the following parameters:
| subject | The subject of the email. | String value |
| body | The body of the email. Plain text or a link to a \[\[NoteFile\]\] (x). | String value |
| showmailto | Show the "mailto" link after the mail body. | true/false (Default: true) |
| variables | A map of placeholder variables. | YAML Object | - |

x) Note that no formatting is supported (only new
lines) ([reason](https://stackoverflow.com/questions/5620324/mailto-link-with-html-body)).

x) Note that no formatting is supported (only new lines) ([reason](https://stackoverflow.com/questions/5620324/mailto-link-with-html-body)).
### Example

![Example](example/email-block-plugin.gif)

## Future features

- Template variables in body + subject

## How to dev
Expand Down
Binary file modified example/email-block-plugin.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 24 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface MailBlockParameters {
subject: string | undefined;
body: string | undefined; // Future version: can be a note, which will be formatted as html for the body
showmailto: boolean | undefined;
variables: { [name: string]: string | undefined }
}

export default class MailBlockPlugin extends Plugin {
Expand Down Expand Up @@ -43,7 +44,7 @@ export default class MailBlockPlugin extends Plugin {
rootEl.createEl("div", {cls: "email-block-info", text: "Subject:"});
rootEl.createEl("div", {cls: "email-block-info-value", text: parameters.subject});
const bodyContent = rootEl.createEl("div", {cls: "email-block-body"});
await this.renderBody(bodyContent, parameters.body);
await this.renderBody(bodyContent, parameters.body, parameters.variables);
const data = "mailto:" + this.encodeToHtml(parameters.to) +
"?subject=" + this.encodeToHtml(parameters.subject) +
"&cc=" + this.encodeToHtml(parameters.cc) +
Expand Down Expand Up @@ -79,11 +80,21 @@ export default class MailBlockPlugin extends Plugin {
parameters.showmailto = true;
}

//Transform internal Link to external
if (parameters.body === undefined) {
parameters.body = "";
}

// Variables
if (parameters.variables === undefined) {
parameters.variables = {};
}
parameters.variables['filename'] = this.app.workspace.getActiveFile()?.basename
const frontmatterData = this.app.metadataCache.getFileCache(app.workspace.getActiveFile()!)?.frontmatter
if (frontmatterData != undefined) {
for (const [key, value] of Object.entries(frontmatterData)) {
parameters.variables[key] = value.toString();
}
}
return parameters;
}

Expand All @@ -99,7 +110,7 @@ export default class MailBlockPlugin extends Plugin {
return address.split(",").join(", ");
}

private async renderBody(bodyContentEl: HTMLElement, bodyContent: string | undefined) {
private async renderBody(bodyContentEl: HTMLElement, bodyContent: string | undefined, variables: { [name: string]: string | undefined }) {
if (bodyContent === undefined) {
return;
}
Expand All @@ -113,9 +124,19 @@ export default class MailBlockPlugin extends Plugin {
);
if (mdFile != null) {
let mdContent = await this.app.vault.read(mdFile);
for (const [variable, value] of Object.entries(variables)) {
if (value != undefined) {
mdContent = mdContent.replace("{{" + variable + "}}", value);
}
}
await MarkdownRenderer.renderMarkdown(mdContent, bodyContentEl, mdFile.path, new Component());
}
} else { // Render line by line as plain text
for (const [variable, value] of Object.entries(variables)) {
if (value != undefined) {
bodyContent = bodyContent?.replace("{{" + variable + "}}", value);
}
}
let lines = bodyContent.split("\n");
lines.forEach(line => {
bodyContentEl.createEl("div", {cls: "email-block-body-line", text: line});
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "email-block-plugin",
"name": "Email code block",
"version": "0.2.0",
"version": "0.3.0",
"minAppVersion": "0.15.0",
"description": "This plugin renders an email code block.",
"author": "JoLeaf",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "email-block-plugin",
"version": "0.2.0",
"version": "0.3.0",
"description": "This plugin renders an email code block.",
"main": "main.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"0.1.0": "0.15.0"
"0.1.0": "0.15.0",
"0.1.1": "0.15.0",
"0.2.0": "0.15.0",
"0.3.0": "0.15.0"
}

0 comments on commit 24b2902

Please sign in to comment.