generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Context menu on articles and shortcuts (closes #15)
- new template variables (closes #14) - new command to refresh articles(closes #16) - new setting: new file location (closes #10)
- Loading branch information
Showing
22 changed files
with
514 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import {RssFeedItem} from "./parser/rssParser"; | ||
import {htmlToMarkdown, MarkdownView, normalizePath, Notice, TextComponent} from "obsidian"; | ||
import {TextInputPrompt} from "./modals/TextInputPrompt"; | ||
import {FILE_NAME_REGEX} from "./consts"; | ||
import {isInVault} from "obsidian-community-lib"; | ||
import RssReaderPlugin from "./main"; | ||
|
||
export async function createNewNote(plugin: RssReaderPlugin, item: RssFeedItem) : Promise<void> { | ||
const activeFile = plugin.app.workspace.getActiveFile(); | ||
let dir = plugin.app.fileManager.getNewFileParent(activeFile ? activeFile.path : "").name; | ||
|
||
if(plugin.settings.saveLocation === "custom") { | ||
dir = plugin.settings.saveLocationFolder; | ||
} | ||
//make sure there are no slashes in the title. | ||
const title = item.title.replace(/[\/\\:]/g, ' '); | ||
const content = htmlToMarkdown(item.content); | ||
|
||
const appliedTemplate = plugin.settings.template | ||
.replace("{{title}}", item.title) | ||
.replace("{{link}}", item.link) | ||
.replace("{{author}}", item.creator) | ||
.replace("{{published}}", item.pubDate) | ||
.replace("{{folder}}", item.folder) | ||
.replace("{{feed}}", item.feed) | ||
.replace("{{content}}", content); | ||
|
||
const inputPrompt = new TextInputPrompt(plugin.app, "Please specify a file name", "cannot contain: * \" \\ / < > : | ?", title, title); | ||
|
||
await inputPrompt | ||
.openAndGetValue(async (text: TextComponent) => { | ||
const value = text.getValue(); | ||
if(value.match(FILE_NAME_REGEX)) { | ||
inputPrompt.setValidationError(text, "that filename is not valid"); | ||
return; | ||
} | ||
const filePath = normalizePath([dir, `${value}.md`].join('/')); | ||
|
||
if (isInVault(plugin.app, filePath, '')) { | ||
inputPrompt.setValidationError(text, "there is already a note with that name"); | ||
return; | ||
} | ||
inputPrompt.close(); | ||
|
||
const file = await plugin.app.vault.create(filePath, appliedTemplate); | ||
|
||
await plugin.app.workspace.activeLeaf.openFile(file, { | ||
state: {mode: 'edit'}, | ||
}) | ||
new Notice("Created note from article"); | ||
}); | ||
} | ||
|
||
export async function pasteToNote(plugin: RssReaderPlugin, item: RssFeedItem) : Promise<void> { | ||
const file = plugin.app.workspace.getActiveFile(); | ||
if (file === null) { | ||
new Notice("no file active"); | ||
return; | ||
} | ||
|
||
const view = plugin.app.workspace.getActiveViewOfType(MarkdownView); | ||
if (view) { | ||
const editor = view.editor; | ||
editor.replaceRange(htmlToMarkdown(item.content), editor.getCursor()); | ||
new Notice("inserted article into note"); | ||
} | ||
} | ||
|
||
export function openInBrowser(item: RssFeedItem) : void { | ||
if (typeof item.link === "string") { | ||
window.open(item.link, '_blank'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes | ||
|
||
import { TAbstractFile, TFolder } from "obsidian"; | ||
import { TextInputSuggest } from "./suggest"; | ||
|
||
export class FolderSuggest extends TextInputSuggest<TFolder> { | ||
getSuggestions(inputStr: string): TFolder[] { | ||
const abstractFiles = this.app.vault.getAllLoadedFiles(); | ||
const folders: TFolder[] = []; | ||
const lowerCaseInputStr = inputStr.toLowerCase(); | ||
|
||
abstractFiles.forEach((folder: TAbstractFile) => { | ||
if ( | ||
folder instanceof TFolder && | ||
folder.path.toLowerCase().contains(lowerCaseInputStr) | ||
) { | ||
folders.push(folder); | ||
} | ||
}); | ||
|
||
return folders; | ||
} | ||
|
||
renderSuggestion(file: TFolder, el: HTMLElement): void { | ||
el.setText(file.path); | ||
} | ||
|
||
selectSuggestion(file: TFolder): void { | ||
this.inputEl.value = file.path; | ||
this.inputEl.trigger("input"); | ||
this.close(); | ||
} | ||
} |
Oops, something went wrong.