Skip to content

Commit

Permalink
Add support for referenced note as body content (version 0.2.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
joleaf committed Dec 30, 2022
1 parent 5449be3 commit 7040362
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 26 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog

All changes to this plugin are listed here.

## 0.2.0 (30.12.2022)

### New

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

## 0.1.1 (28.12.2022)

### Changed
Expand Down
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -30,19 +31,29 @@ body: "Hey info,
```
````

... with a referenced note as body content:
````
```email
to: [email protected]
subject: My Subject
body: [[MyMail4711]]
```
````

### Parameter

You can customize the view with the following parameters:

| Parameter | Description | Values |
|------------|--------------------------------------------------------------------|----------------------------|
| to | The main receiver of the mail. Multiple receiver seperated by ",". | String value |
| cc | The cc receiver of the mail. Multiple receiver seperated by ",". | String value |
| bcc | The bcc receiver of the mail. Multiple receiver seperated by ",". | String value |
| subject | The subject of the email. | String value |
| body | The body of the email. | String value |
| showmailto | Show the "mailto" link after the mail body. | true/false (Default: true) |
| Parameter | Description | Values |
|------------|------------------------------------------------------------------------|----------------------------|
| to | The main receiver of the mail. Multiple receiver seperated by ",". | String value |
| cc | The cc receiver of the mail. Multiple receiver seperated by ",". | String value |
| bcc | The bcc receiver of the mail. Multiple receiver seperated by ",". | String value |
| 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) |

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)
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.
38 changes: 22 additions & 16 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Plugin, parseYaml} from "obsidian";
import {Plugin, parseYaml, MarkdownRenderer, Component} from "obsidian";

interface MailBlockParameters {
to: string | undefined;
Expand Down Expand Up @@ -43,12 +43,12 @@ 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"});
this.renderBody(bodyContent, parameters.body);
await this.renderBody(bodyContent, parameters.body);
const data = "mailto:" + this.encodeToHtml(parameters.to) +
"?subject=" + this.encodeToHtml(parameters.subject) +
"&cc=" + this.encodeToHtml(parameters.cc) +
"&bcc=" + this.encodeToHtml(parameters.bcc) +
"&body=" + this.encodeToHtml(parameters.body);
"&body=" + this.encodeToHtml(bodyContent.innerText);
if (parameters.showmailto) {
rootEl.createEl("a", {href: data, text: "Mailto"});
}
Expand Down Expand Up @@ -82,14 +82,6 @@ export default class MailBlockPlugin extends Plugin {
//Transform internal Link to external
if (parameters.body === undefined) {
parameters.body = "";
} else if (parameters.body.startsWith("[[")) {
parameters.body = parameters.body.substring(2, parameters.body.length - 2);
// @ts-ignore
parameters.url = this.app.metadataCache.getFirstLinkpathDest(
parameters.body,
""
).path;
// TODO: for future version: transform content to html!
}

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

private renderBody(bodyContentEl: HTMLElement, bodyContent: string | undefined) {
private async renderBody(bodyContentEl: HTMLElement, bodyContent: string | undefined) {
if (bodyContent === undefined) {
return;
}
let lines = bodyContent.split("\n");
lines.forEach(line => {
bodyContentEl.createEl("div", {cls: "email-block-body-line", text: line});
})
// render a markdown file
if (bodyContent.startsWith("[[")) {
bodyContent = bodyContent.substring(2, bodyContent.length - 2);

const mdFile = this.app.metadataCache.getFirstLinkpathDest(
bodyContent,
""
);
if (mdFile != null) {
let mdContent = await this.app.vault.read(mdFile);
await MarkdownRenderer.renderMarkdown(mdContent, bodyContentEl, mdFile.path, new Component());
}
} else { // Render line by line as plain text
let lines = bodyContent.split("\n");
lines.forEach(line => {
bodyContentEl.createEl("div", {cls: "email-block-body-line", text: line});
})
}
}

private encodeToHtml(rawStr: string | undefined) {
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.1.1",
"version": "0.2.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.1.1",
"version": "0.2.0",
"description": "This plugin renders an email code block.",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 7040362

Please sign in to comment.