-
-
Notifications
You must be signed in to change notification settings - Fork 186
feat: option to insert template or capture links into the frontmatter of active note #1276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import type { LinkPlacement, LinkType } from "../../types/linkPlacement"; | |
| import { | ||
| normalizeAppendLinkOptions, | ||
| placementSupportsEmbed, | ||
| placementSupportsFrontmatter, | ||
| } from "../../types/linkPlacement"; | ||
| import { getAllFolderPathsInVault } from "../../utilityObsidian"; | ||
| import { sortFolderPathsByTree } from "../../utils/folder-sorting"; | ||
|
|
@@ -819,6 +820,7 @@ export class CaptureChoiceBuilder extends ChoiceBuilder { | |
| dropdown.addOption("afterSelection", "After selection"); | ||
| dropdown.addOption("endOfLine", "End of line"); | ||
| dropdown.addOption("newLine", "New line"); | ||
| dropdown.addOption("inFrontmatter", "In frontmatter property"); | ||
|
|
||
| dropdown.setValue(normalizedOptions.placement); | ||
| dropdown.onChange((value: LinkPlacement) => { | ||
|
|
@@ -875,6 +877,35 @@ export class CaptureChoiceBuilder extends ChoiceBuilder { | |
| }); | ||
| }); | ||
| } | ||
|
|
||
| if (placementSupportsFrontmatter(normalizedOptions.placement)) { | ||
| const linkTypeSetting: Setting = new Setting(this.contentEl); | ||
| const current = typeof this.choice.appendLink !== "boolean" ? this.choice.appendLink.frontmatterProperty : ''; | ||
| linkTypeSetting | ||
| .setName("Frontmatter property") | ||
| .setDesc("Choose the frontmatter property to insert the link into.") | ||
| .addText((text) => { | ||
| text.setValue(current ?? '') | ||
| text.onChange((value: string) => { | ||
| const currentValue = this.choice.appendLink; | ||
| const requireActiveFile = | ||
| typeof currentValue === "boolean" | ||
| ? normalizedOptions.requireActiveFile | ||
| : currentValue.requireActiveFile; | ||
| const placement = | ||
| typeof currentValue === "boolean" | ||
| ? normalizedOptions.placement | ||
| : currentValue.placement; | ||
|
|
||
| this.choice.appendLink = { | ||
| enabled: true, | ||
| placement, | ||
| requireActiveFile, | ||
| frontmatterProperty: value, | ||
| }; | ||
| }); | ||
| }); | ||
| } | ||
|
Comment on lines
+881
to
+909
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing When the user changes the frontmatter property (lines 900-905), the code reconstructs 🔧 Proposed fix if (placementSupportsFrontmatter(normalizedOptions.placement)) {
- const linkTypeSetting: Setting = new Setting(this.contentEl);
+ const frontmatterPropertySetting: Setting = new Setting(this.contentEl);
const current = typeof this.choice.appendLink !== "boolean" ? this.choice.appendLink.frontmatterProperty : '';
- linkTypeSetting
+ frontmatterPropertySetting
.setName("Frontmatter property")
.setDesc("Choose the frontmatter property to insert the link into.")
.addText((text) => {
text.setValue(current ?? '')
text.onChange((value: string) => {
const currentValue = this.choice.appendLink;
const requireActiveFile =
typeof currentValue === "boolean"
? normalizedOptions.requireActiveFile
: currentValue.requireActiveFile;
const placement =
typeof currentValue === "boolean"
? normalizedOptions.placement
: currentValue.placement;
+ const linkType =
+ typeof currentValue === "boolean"
+ ? normalizedLinkType
+ : currentValue.linkType ?? normalizedLinkType;
this.choice.appendLink = {
enabled: true,
placement,
requireActiveFile,
+ linkType,
frontmatterProperty: value,
};
});
});
}🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Same issue as in the capture choice builder: when a user toggles between "Enabled (requires active file)" and "Enabled (skip if no active file)" while placement is "In frontmatter property", the (Refers to lines 336-353) Was this helpful? React with 👍 or 👎 to provide feedback. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -553,9 +553,9 @@ export function insertLinkWithPlacement( | |
| app: App, | ||
| text: string, | ||
| mode: LinkPlacement = "replaceSelection", | ||
| options: { requireActiveView?: boolean; } = {}, | ||
| options: { requireActiveView?: boolean; frontmatterProperty?: string} = {}, | ||
| ) { | ||
| const { requireActiveView = true } = options; | ||
| const { requireActiveView = true, frontmatterProperty = '' } = options; | ||
| const view = app.workspace.getActiveViewOfType(MarkdownView); | ||
| if (!view) { | ||
| const message = "Cannot append link because no active Markdown view is available."; | ||
|
|
@@ -584,6 +584,33 @@ export function insertLinkWithPlacement( | |
| editor.replaceSelection(text); | ||
| return; | ||
| } | ||
|
|
||
| ////////////////////////////////////////////////////////////////// | ||
| // FRONTMATTER-SELECTION | ||
| ////////////////////////////////////////////////////////////////// | ||
|
|
||
| if (mode === "inFrontmatter") { | ||
| if(!frontmatterProperty) { | ||
| const message = "Invalid frontmatter property: " + frontmatterProperty; | ||
| throw new Error(message); | ||
| } | ||
| const file = view.file; | ||
| if(!file) { | ||
| const message = "Could not find file of active view"; | ||
| throw new Error(message); | ||
| } | ||
| app.fileManager.processFrontMatter(file, (frontmatter) => { | ||
| if (frontmatter[frontmatterProperty] === undefined) { | ||
| frontmatter[frontmatterProperty] = [] | ||
| } | ||
| if (!Array.isArray(frontmatter[frontmatterProperty])) { | ||
| const message = "Could not add into non array property:" + frontmatterProperty; | ||
| throw new Error(message) | ||
| } | ||
| frontmatter[frontmatterProperty].push(text) | ||
| }) | ||
| return | ||
|
Comment on lines
+602
to
+612
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴
Since Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
|
Comment on lines
+592
to
+613
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing The 🐛 Proposed fix- if (mode === "inFrontmatter") {
- if(!frontmatterProperty) {
+ if (mode === "inFrontmatter") {
+ if (!frontmatterProperty) {
const message = "Invalid frontmatter property: " + frontmatterProperty;
throw new Error(message);
}
const file = view.file;
- if(!file) {
+ if (!file) {
const message = "Could not find file of active view";
throw new Error(message);
}
- app.fileManager.processFrontMatter(file, (frontmatter) => {
+ await app.fileManager.processFrontMatter(file, (frontmatter) => {
if (frontmatter[frontmatterProperty] === undefined) {
- frontmatter[frontmatterProperty] = []
+ frontmatter[frontmatterProperty] = [];
}
if (!Array.isArray(frontmatter[frontmatterProperty])) {
const message = "Could not add into non array property:" + frontmatterProperty;
- throw new Error(message)
+ throw new Error(message);
}
- frontmatter[frontmatterProperty].push(text)
- })
- return
+ frontmatter[frontmatterProperty].push(text);
+ });
+ return;
}Note: This also requires making -export function insertLinkWithPlacement(
+export async function insertLinkWithPlacement(And updating the call site at line 761 to await it. 🤖 Prompt for AI Agents |
||
|
|
||
| ////////////////////////////////////////////////////////////////// | ||
| // ALL OTHER MODES NEED EXPLICIT POSITION CALCULATION | ||
|
|
@@ -735,7 +762,7 @@ export function insertFileLinkToActiveView( | |
| app, | ||
| linkText, | ||
| linkOptions.placement, | ||
| { requireActiveView: false }, | ||
| { requireActiveView: false, frontmatterProperty : linkOptions.frontmatterProperty }, | ||
| ); | ||
|
|
||
| return true; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡
frontmatterPropertysilently dropped when toggling link mode between required/optionalWhen a user toggles the "Enabled (requires active file)" / "Enabled (skip if no active file)" dropdown while the placement is set to "In frontmatter property", the onChange handler constructs a new
appendLinkobject that includesplacement,requireActiveFile, andlinkType, but omitsfrontmatterProperty. ThenormalizedOptionscaptured at the top ofaddAppendLinkSetting()contains thefrontmatterPropertyvalue (vianormalizeAppendLinkOptionsatsrc/types/linkPlacement.ts:85), but it is not forwarded. Afterthis.reload(), the frontmatter property text field resets to empty and the user's configured property name is lost.The same issue exists in
templateChoiceBuilder.ts:336-353.(Refers to lines 791-806)
Was this helpful? React with 👍 or 👎 to provide feedback.