Skip to content

Commit

Permalink
adds template file and override files setting
Browse files Browse the repository at this point in the history
  • Loading branch information
StrangeGirlMurph committed Dec 2, 2023
1 parent c05dc37 commit c70cb6d
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 198 deletions.
Binary file removed docs/assets/tempalte-settings.png
Binary file not shown.
Binary file added docs/assets/template-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/templater-setting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Changelog

## [2.3.3](https://github.com/StrangeGirlMurph/obsidian-wikipedia-search/releases/tag/2.3.3) (02.12.2023)
- Adds the template file option.
- Adds the `Override files` setting
- Fixes some bugs.

## [2.3.2](https://github.com/StrangeGirlMurph/obsidian-wikipedia-search/releases/tag/2.3.2) (27.11.2023)
- Adds the `Create article note` command
- Adds the `Open created article notes` setting
- Adds the `Create article note` command.
- Adds the `Open created article notes` setting.

## [2.3.1](https://github.com/StrangeGirlMurph/obsidian-wikipedia-search/releases/tag/2.3.1) (18.11.2023)
- Introduces the new documentation and links it from the settings page.
Expand Down
1 change: 1 addition & 0 deletions docs/help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If you tried finding an answer to your question in this documentation but still need help with something feel free to ask the community and me for help in the [Q&A in the GitHub Discussions Tab](https://github.com/StrangeGirlMurph/obsidian-wikipedia-search/discussions/categories/q-a).
27 changes: 19 additions & 8 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ The default vault folder where articles notes should be created. This only gets
Default: `/` (the root folder)

## Template settings
Each user has a list of templates for the inserts that this plugin creates when linking articles. "Linking" in this context might be a bit misdirecting though given the fact that this plugin can do more than just link the article with a url. The templates can be separated into one default template and multiple additional templates. Templates can be deleted with the `-` button at the end. The default template can't be deleted.
Each user has a list of templates for the inserts that this plugin creates when [linking articles](commands.md#link-article) or [creating article notes](commands.md#create-article-note). The templates can be separated into one default template and multiple additional templates. Templates can be deleted with the `-` button at the end. The default template can't be deleted. You can create new templates with the `+` button at the bottom right.

The settings for a template can be separated into the following three parts:

*Note: You can only have up to 20 templates. It's a limitation that isn't really justified. I just thought adding this would be a good idea for your own good. If you need more than 20 leave a message.*

??? info "Screenshot"

![template settings screenshot](assets/tempalte-settings.png)
![template settings screenshot](assets/template-settings.png)

### Template name
The first field of each template sets the name of the template.The name of the default template can't be changed (that's why that field is disabled) while the additional templates can have any name.
The first field of each template sets the name of the template. The name of the default template can't be changed (that's why that field is disabled) while the additional templates can have any name.

Default: `Additional Template`

### Creates note & Custom note path
The middle part of a templates settings is all about note creation. It consist of a toggle and a text field that appears when the toggle in the "on" state. The toggle controls whether or not the template should create a new note with the articles title as its name, paste the insert in there and link the newly created note instead of directly pasting the insert into the current note. This allows for creating notes for wikipedia articles. We call templates that create new notes "note templates" and templates that doesn't "inline templates".
The middle part of a templates settings is all about note creation. It consist of a toggle and a text field that appears when the "creates a note" toggle is in the "on" state. The toggle controls whether or not the template should create a new note with the articles title as its name, paste the insert in there and link the newly created note instead of directly pasting the insert into the current note. This allows for creating notes for wikipedia articles. We call templates that create new notes "note templates" and templates that doesn't "inline templates".

By default all the notes will be created at the [default note path](#default-note-path). The appearing text field lets you customize the location for any given template. Leave it empty to use the default note path.

Default: `false` for the toggle (inline) and `` (empty) for the custom note path
Default: `false` as in "inline" for the toggle and `` (empty) for the custom note path

### Template string
The last and most important part of any template is its template string. You can edit it over the big text field at the end. The template string is the blueprint for the insert. It can be any kind of string containing line breaks. The plugin recognizes the following character sequences and replaces all occurrences with the following:
The last and most important part of any template is its template string. The template string is the blueprint for the insert. This last section consists of a toggle and an input field which is either a big text or a file search field. The toggle sets whether or not the template string is declared directly in the settings tab in the big text field or separately in a file which the file search field references instead. Template files are only supported for note templates. That's why the toggle disappears and the input automatically switches to the text field once you switch to an inline template.

The template string can be any kind of string containing line breaks and whatever you can think of. The plugin recognizes the following character sequences and replaces all occurrences with the corresponding data:

- `{title}` The articles title or current selection (based on [this setting](#use-article-title-instead-of-selection)).
- `{description}` The articles description if available. If not all occurrences will be removed with a notice.
Expand All @@ -63,6 +63,12 @@ The last and most important part of any template is its template string. You can

Default: `[{title}]({url})` for inline templates and `{thumbnail}\n[{title}]({url}): {intro}` for note templates

??? tip

You can also use [Templater](https://github.com/SilentVoid13/Templater) Syntax in the template string of note templates to make the article notes even better! Just [install](obsidian://show-plugin?id=templater) and enable the Templater Plugin and enable its "Trigger Templater on new file creation" setting:

![Templater Setting](assets/templater-setting.png)

## Workflow optimization settings

??? info "Screenshot"
Expand Down Expand Up @@ -118,4 +124,9 @@ Default: `false`
### Open created article notes
Whether or not to open the newly created article notes directly after creating them. Follows the [article tab placement setting](#article-tab-placement).

Default: `false`

### Override files
Whether or not to override existing files when creating article notes.

Default: `false`
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ nav:
- "installation.md"
- "settings.md"
- "commands.md"
- "help.md"
- "roadmap.md"
- "changelog.md"
- "support.md"
Expand Down
31 changes: 14 additions & 17 deletions src/commands/createArticleNotes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, TFile } from "obsidian";
import { App, Notice, TFile } from "obsidian";
import { Template, WikipediaSearchSettings } from "../settings";
import { Article } from "src/utils/searchModal";
import { SearchModal } from "src/utils/searchModal";
Expand All @@ -10,27 +10,14 @@ export class CreateArticleNoteModal extends SearchModal {
async onChooseSuggestion(article: Article) {
const templates = this.settings.templates.filter((template) => template.createNote);
if (templates.length > 1) {
new CreateArticleNoteTemplateModal(this.app, this.settings, this.editor!, article).open();
new CreateArticleNoteTemplateModal(this.app, this.settings, this.editor!, article, true).open();
} else {
createArticleNote(this.app, this.settings, article, templates[0]);
}
}
}

class CreateArticleNoteTemplateModal extends TemplateModal {
renderSuggestion(template: Template, el: HTMLElement) {
el.createEl("div", { text: `${template.name}` });
el.createEl("small", {
text: template.templateString.replaceAll("\n", "\\n"),
});
}

async getSuggestions(query: string): Promise<Template[]> {
return this.settings.templates
.filter((template) => template.createNote)
.filter((template) => template.name.toLowerCase().includes(query.toLowerCase()));
}

async onChooseSuggestion(template: Template) {
createArticleNote(this.app, this.settings, this.article, template);
}
Expand All @@ -42,12 +29,22 @@ async function createArticleNote(
article: Article,
template: Template
) {
const insert = await generateInsert(settings, article, template.templateString, "");
let templateString = template.templateString;
if (template.useTemplateFile) {
const templateFile = app.vault.getAbstractFileByPath(template.templateFilePath);
if (!templateFile || !(templateFile instanceof TFile)) {
new Notice(`Aborting! Template file '${template.templateFilePath}' not found!`);
return;
}
templateString = await app.vault.read(templateFile);
}
const insert = await generateInsert(settings, article, templateString, "");
const path = await createNoteInFolder(
app,
article.title,
insert,
template.customPath || settings.defaultNotePath
template.customPath || settings.defaultNotePath,
settings.overrideFiles
);
if (!path) return;
if (settings.openCreatedNotes) {
Expand Down
16 changes: 2 additions & 14 deletions src/commands/linkArticles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ export class LinkArticleModal extends SearchModal {
}

class LinkArticleTemplateModal extends TemplateModal {
renderSuggestion(template: Template, el: HTMLElement) {
el.createEl("div", { text: `${template.name} ${template.createNote ? "(note)" : "(inplace)"}` });
el.createEl("small", {
text: template.templateString.replaceAll("\n", "\\n"),
});
}

async getSuggestions(query: string): Promise<Template[]> {
return this.settings.templates.filter((template) =>
template.name.toLowerCase().includes(query.toLowerCase())
);
}

async onChooseSuggestion(template: Template) {
insertLink(app, this.editor, this.settings, this.article, template);
}
Expand All @@ -49,7 +36,8 @@ async function insertLink(
app,
article.title,
insert,
template.customPath === "" ? settings.defaultNotePath : template.customPath
template.customPath === "" ? settings.defaultNotePath : template.customPath,
settings.overrideFiles
);
if (path == null) return;

Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class WikipediaSearchPlugin extends Plugin {
name: "Create Article Note",
callback: () => {
if (this.settings.templates.filter((template) => template.createNote).length === 0) {
new Notice("You have to create a note template first! (see the docs)");
new Notice("To use this command you have to create a note template first!");
return;
}
new CreateArticleNoteModal(this.app, this.settings).open();
Expand Down
Loading

0 comments on commit c70cb6d

Please sign in to comment.