Skip to content

Commit

Permalink
feature: wrapper template
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Jan 30, 2024
1 parent 6584d5e commit 6954a09
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changelog

- [feature] Wrapper: support template for metadata (file properties).
- [feature] New Setting: Proper nouns, which are ignored in title case. e.g., USA, UFO.
- [updated] paste Zotero note: if regex fail to match, return original text in clipboard.
- [updated] General: commands support Chinese language. 命令支持中文显示
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"rollup": "^2.32.1",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"dependencies": {
"handlebars": "^4.7.6"
}
}
}
19 changes: 12 additions & 7 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Editor, MarkdownView, EditorPosition, App, requestUrl, TFile, Notice } from "obsidian";
import { FormatSettings } from "src/setting";
import { compile as compileTemplate, TemplateDelegate as Template } from 'handlebars';

import { off } from "process";

Expand Down Expand Up @@ -266,16 +267,20 @@ String.prototype.format = function (args: any) {
return result;
};

export function textWrapper(editor: Editor, view: MarkdownView, prefix: string, suffix: string): void {
prefix = prefix.replace(/\\n/g, "\n");
suffix = suffix.replace(/\\n/g, "\n");
export function textWrapper(editor: Editor, view: MarkdownView, prefix_setting: string, suffix_setting: string): void {
// @ts-ignore
const metaProperties = view.metadataEditor.properties;
let meta: Record<string, any> = {};
for (const m of metaProperties) { meta[m.key] = m.value; }

let prefix_template = compileTemplate(prefix_setting.replace(/\\n/g, "\n"), { noEscape: true })
let suffix_template = compileTemplate(suffix_setting.replace(/\\n/g, "\n"), { noEscape: true })

const prefix = prefix_template(meta);
const suffix = suffix_template(meta);
const PL = prefix.length; // Prefix Length
const SL = suffix.length; // Suffix Length

if (!view) {
return;
}

let selectedText = editor.somethingSelected() ? editor.getSelection() : "";

let last_cursor = editor.getCursor(); // the cursor that at the last position of doc
Expand Down
9 changes: 6 additions & 3 deletions src/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ export class TextFormatSettingTab extends PluginSettingTab {
containerEl.createEl("h3", { text: "Wrapper" });
const wrapperRuleDesc = document.createDocumentFragment();
wrapperRuleDesc.append(
"<Wrapper Name> <Prefix> <Suffix>",
"<Wrapper Name> <Prefix Template> <Suffix Template>",
document.createDocumentFragment().createEl("br"),
"Note: To make sure the command is valid in Command Palette, you need to **reload/reopen** Obsidian App."
"Template for metadata (file properties) is supported with Handlebars syntax. For example, `{{link}}` will be replaced with the value of current file's property `link`.",
document.createDocumentFragment().createEl("br"),
`Note: To make sure the command is valid in Command Palette, you need to re-enable the plugin "Text Format" or reload/reopen Obsidian App.`
);
new Setting(this.containerEl)
.setName("Add new wrapper")
Expand Down Expand Up @@ -296,7 +298,8 @@ export class TextFormatSettingTab extends PluginSettingTab {
"The URL that plugin will send a POST and replace with return.\n" +
"The return json should have two attribution: `text` and `notification`. " +
"If `text` exist then `text` will replace the selection, or do nothing.\n" +
"If `notification` exist then Send a notice if this string, or do nothing."
"If `notification` exist then Send a notice if this string, or do nothing.\n" +
`(Note: To make sure the command is valid in Command Palette, you need to re-enable the plugin "Text Format" or reload/reopen Obsidian App.)`
)
.addButton((button: ButtonComponent) => {
button.setTooltip("Add new request")
Expand Down

0 comments on commit 6954a09

Please sign in to comment.