Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/FormatSyntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ title: Format syntax
| `{{TEMPLATE:<TEMPLATEPATH>}}` | Include templates in your `format`. Supports Templater syntax. |
| `{{GLOBAL_VAR:<name>}}` | Inserts the value of a globally defined snippet from QuickAdd settings. Snippet values can include other QuickAdd tokens (e.g., `{{VALUE:...}}`, `{{VDATE:...}}`) and are processed by the usual formatter passes. Names match case‑insensitively in the token. |
| `{{MVALUE}}` | Math modal for writing LaTeX. Use CTRL + Enter to submit. |
| `{{FIELD:<FIELDNAME>}}` | Suggest the values of `FIELDNAME` anywhere `{{FIELD:FIELDNAME}}` is used. Fields are YAML fields, and the values represent any value this field has in your vault. If there exists no such field or value, you are instead prompted to enter one.<br/><br/>**Enhanced Filtering Options:**<br/>• `{{FIELD:fieldname\|folder:path/to/folder}}` - Only suggest values from files in specific folder<br/>• `{{FIELD:fieldname\|tag:tagname}}` - Only suggest values from files with specific tag<br/>• `{{FIELD:fieldname\|inline:true}}` - Include Dataview inline fields (fieldname:: value)<br/>• `{{FIELD:fieldname\|exclude-folder:templates}}` - Exclude values from files in specific folder<br/>• `{{FIELD:fieldname\|exclude-tag:deprecated}}` - Exclude values from files with specific tag<br/>• `{{FIELD:fieldname\|exclude-file:example.md}}` - Exclude values from specific file<br/>• `{{FIELD:fieldname\|default:Status - To Do}}` - Prepend a default suggestion; the modal placeholder shows it and pressing Enter accepts it.<br/>• `{{FIELD:fieldname\|default:Draft\|default-empty:true}}` - Only add the default when no matching values are found.<br/>• `{{FIELD:fieldname\|default:Draft\|default-always:true}}` - Keep the default first even if other suggestions exist.<br/>• Combine filters: `{{FIELD:fieldname\|folder:daily\|tag:work\|exclude-folder:templates\|inline:true}}`<br/>• Multiple exclusions: `{{FIELD:fieldname\|exclude-folder:templates\|exclude-folder:archive}}`<br/><br/>This is currently in beta, and the syntax can change—leave your thoughts [here](https://github.com/chhoumann/quickadd/issues/337). |
| `{{FIELD:<FIELDNAME>}}` | Suggest the values of `FIELDNAME` anywhere `{{FIELD:FIELDNAME}}` is used. Fields are YAML fields, and the values represent any value this field has in your vault. If there exists no such field or value, you are instead prompted to enter one.<br/><br/>**Enhanced Filtering Options:**<br/>• `{{FIELD:fieldname\|folder:path/to/folder}}` - Only suggest values from files in specific folder<br/>• `{{FIELD:fieldname\|tag:tagname}}` - Only suggest values from files with specific tag<br/>• `{{FIELD:fieldname\|inline:true}}` - Include Dataview inline fields (fieldname:: value)<br/>• `{{FIELD:fieldname\|inline:true\|inline-code-blocks:ad-note}}` - Include inline fields inside specific fenced code blocks (opt-in)<br/>• `{{FIELD:fieldname\|exclude-folder:templates}}` - Exclude values from files in specific folder<br/>• `{{FIELD:fieldname\|exclude-tag:deprecated}}` - Exclude values from files with specific tag<br/>• `{{FIELD:fieldname\|exclude-file:example.md}}` - Exclude values from specific file<br/>• `{{FIELD:fieldname\|default:Status - To Do}}` - Prepend a default suggestion; the modal placeholder shows it and pressing Enter accepts it.<br/>• `{{FIELD:fieldname\|default:Draft\|default-empty:true}}` - Only add the default when no matching values are found.<br/>• `{{FIELD:fieldname\|default:Draft\|default-always:true}}` - Keep the default first even if other suggestions exist.<br/>• Combine filters: `{{FIELD:fieldname\|folder:daily\|tag:work\|exclude-folder:templates\|inline:true\|inline-code-blocks:ad-note}}`<br/>• Multiple exclusions: `{{FIELD:fieldname\|exclude-folder:templates\|exclude-folder:archive}}`<br/><br/>This is currently in beta, and the syntax can change—leave your thoughts [here](https://github.com/chhoumann/quickadd/issues/337). |
| `{{selected}}` | The selected text in the current editor. Will be empty if no active editor exists. |
| `{{CLIPBOARD}}` | The current clipboard content. Will be empty if clipboard access fails due to permissions or security restrictions. |
| `{{RANDOM:<length>}}` | Generates a random alphanumeric string of the specified length (1-100). Useful for creating unique identifiers, block references, or temporary codes. Example: `{{RANDOM:6}}` generates something like `3YusT5`. |
Expand Down
13 changes: 13 additions & 0 deletions docs/docs/QuickAddAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ Retrieves all unique values for a specific field across your vault.
- `folder`: Only search in specific folder (e.g., "daily/notes")
- `tags`: Only search in files with specific tags (array)
- `includeInline`: Include Dataview inline fields (default: false)
- `includeInlineCodeBlocks`: Include inline fields inside specific fenced code block types when `includeInline` is true (e.g., `["ad-note"]`)

**Returns:** Promise resolving to sorted array of unique field values

Expand Down Expand Up @@ -774,6 +775,18 @@ const clients = await quickAddApi.fieldSuggestions.getFieldValues(
);
```

Include inline fields in specific code block types:
```javascript
const ids = await quickAddApi.fieldSuggestions.getFieldValues(
"Id",
{
folder: "work/projects",
includeInline: true,
includeInlineCodeBlocks: ["ad-note"]
}
);
```

### `clearCache(fieldName?: string): void`
Clears the field suggestions cache for better performance.

Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const FORMAT_SYNTAX: string[] = [
"{{field:<fieldname>|folder:<path>}}",
"{{field:<fieldname>|tag:<tagname>}}",
"{{field:<fieldname>|inline:true}}",
"{{field:<fieldname>|inline:true|inline-code-blocks:ad-note}}",
LINKCURRENT_SYNTAX,
FILENAMECURRENT_SYNTAX,
"{{macro:<macroname>}}",
Expand Down
93 changes: 93 additions & 0 deletions src/quickAddApi.fieldSuggestions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { App, TFile } from "obsidian";
import type { IChoiceExecutor } from "./IChoiceExecutor";
import type QuickAdd from "./main";
import { QuickAddApi } from "./quickAddApi";

vi.mock("./quickAddSettingsTab", () => ({
DEFAULT_SETTINGS: {},
QuickAddSettingsTab: class {},
}));

vi.mock("./formatters/completeFormatter", () => ({
CompleteFormatter: class CompleteFormatterMock {},
}));

vi.mock("obsidian-dataview", () => ({
getAPI: vi.fn(),
}));

const INLINE_CONTENT = `
Id:: 343434

\`\`\`ad-note
Id:: 121212
\`\`\`

\`\`\`js
Id:: 999999
\`\`\`
`;

function createApp(content: string): App {
const file = { path: "QuickAdd-Issue-998/repro.md" } as TFile;
return {
vault: {
getMarkdownFiles: () => [file],
read: vi.fn(async () => content),
},
metadataCache: {
getFileCache: vi.fn(() => ({ frontmatter: {} })),
},
} as unknown as App;
}

describe("QuickAddApi.fieldSuggestions.getFieldValues", () => {
let variables: Map<string, unknown>;
let choiceExecutor: IChoiceExecutor;
let plugin: QuickAdd;

beforeEach(() => {
variables = new Map<string, unknown>();
choiceExecutor = {
execute: vi.fn(),
variables,
} as unknown as IChoiceExecutor;
plugin = {} as QuickAdd;
});

it("keeps code-block values excluded by default when includeInline is true", async () => {
const app = createApp(INLINE_CONTENT);
const api = QuickAddApi.GetApi(app, plugin, choiceExecutor);

const result = await api.fieldSuggestions.getFieldValues("Id", {
includeInline: true,
});

expect(result).toEqual(["343434"]);
});

it("includes allowlisted code-block values when includeInlineCodeBlocks is provided", async () => {
const app = createApp(INLINE_CONTENT);
const api = QuickAddApi.GetApi(app, plugin, choiceExecutor);

const result = await api.fieldSuggestions.getFieldValues("Id", {
includeInline: true,
includeInlineCodeBlocks: ["ad-note"],
});

expect(result).toEqual(["121212", "343434"]);
});

it("does not scan inline values when includeInline is false", async () => {
const app = createApp(INLINE_CONTENT);
const api = QuickAddApi.GetApi(app, plugin, choiceExecutor);

const result = await api.fieldSuggestions.getFieldValues("Id", {
includeInline: false,
includeInlineCodeBlocks: ["ad-note"],
});

expect(result).toEqual([]);
});
});
8 changes: 8 additions & 0 deletions src/quickAddApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,17 @@ export class QuickAddApi {
folder?: string;
tags?: string[];
includeInline?: boolean;
includeInlineCodeBlocks?: string[];
},
) => {
const inlineCodeBlocks = options?.includeInlineCodeBlocks
?.map((value) => value.trim().toLowerCase())
.filter((value) => value.length > 0);
const filters = {
folder: options?.folder,
tags: options?.tags,
inline: options?.includeInline ?? false,
inlineCodeBlocks,
};

// Get all markdown files and apply filters
Expand Down Expand Up @@ -578,6 +583,9 @@ export class QuickAddApi {
const inlineValues = InlineFieldParser.getFieldValues(
content,
fieldName,
{
includeCodeBlocks: inlineCodeBlocks,
},
);
inlineValues.forEach((v) => values.add(v));
}
Expand Down
15 changes: 14 additions & 1 deletion src/utils/FieldSuggestionParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ describe("FieldSuggestionParser", () => {
});
});

it("should parse inline code block allowlist filter", () => {
const result = FieldSuggestionParser.parse(
"fieldname|inline:true|inline-code-blocks:ad-note, dataview",
);
expect(result).toEqual({
fieldName: "fieldname",
filters: {
inline: true,
inlineCodeBlocks: ["ad-note", "dataview"],
},
});
});

it("should parse field name with multiple filters", () => {
const result = FieldSuggestionParser.parse(
"fieldname|folder:daily|tag:work|inline:true",
Expand Down Expand Up @@ -182,4 +195,4 @@ describe("FieldSuggestionParser", () => {
});
});
});
});
});
12 changes: 12 additions & 0 deletions src/utils/FieldSuggestionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface FieldFilter {
folder?: string;
tags?: string[];
inline?: boolean;
inlineCodeBlocks?: string[];
defaultValue?: string;
defaultEmpty?: boolean;
defaultAlways?: boolean;
Expand Down Expand Up @@ -54,6 +55,17 @@ export class FieldSuggestionParser {
case "inline":
filters.inline = filterValue.toLowerCase() === "true";
break;
case "inline-code-blocks":
if (!filters.inlineCodeBlocks) {
filters.inlineCodeBlocks = [];
}
filters.inlineCodeBlocks.push(
...filterValue
.split(",")
.map((value) => value.trim().toLowerCase())
.filter((value) => value.length > 0),
);
break;
case "default":
filters.defaultValue = filterValue;
break;
Expand Down
30 changes: 30 additions & 0 deletions src/utils/FieldValueCollector.issue671.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,34 @@ describe("Issue #671 - {{FIELD:tags}} suggestions", () => {
expect.arrayContaining(["ai/technology", "cook/hoven"]),
);
});

it("includes inline fields inside allowlisted code blocks only when configured", async () => {
const app = new App();
const file = { path: "folder/note.md" } as any;

app.vault.getMarkdownFiles = () => [file];
app.metadataCache.getFileCache = () => ({ frontmatter: {} } as any);
app.vault.read = vi.fn(async () => `
Id:: 343434
\`\`\`ad-note
Id:: 121212
\`\`\`
\`\`\`js
Id:: 999999
\`\`\`
`);

const withoutCodeBlockAllowlist = await collectFieldValuesProcessed(
app,
"Id",
{ inline: true },
);
const withCodeBlockAllowlist = await collectFieldValuesProcessed(app, "Id", {
inline: true,
inlineCodeBlocks: ["ad-note"],
});

expect(withoutCodeBlockAllowlist).toEqual(["343434"]);
expect(withCodeBlockAllowlist).toEqual(["121212", "343434"]);
});
});
6 changes: 6 additions & 0 deletions src/utils/FieldValueCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export function generateFieldCacheKey(filters: FieldFilter): string {
if (filters.folder) parts.push(`folder:${filters.folder}`);
if (filters.tags) parts.push(`tags:${filters.tags.join(",")}`);
if (filters.inline) parts.push("inline:true");
if (filters.inlineCodeBlocks?.length) {
parts.push(`inline-code-blocks:${filters.inlineCodeBlocks.join(",")}`);
}
if (filters.caseSensitive) parts.push("case-sensitive:true");
if (filters.excludeFolders)
parts.push(`exclude-folders:${filters.excludeFolders.join(",")}`);
Expand Down Expand Up @@ -243,6 +246,9 @@ async function collectFieldValuesManually(
const inlineValues = InlineFieldParser.getFieldValues(
content,
fieldName,
{
includeCodeBlocks: filters.inlineCodeBlocks,
},
);
inlineValues.forEach((s) => {
const t = String(s).trim();
Expand Down
59 changes: 52 additions & 7 deletions src/utils/InlineFieldParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ field2:: value2
expect(result.get("field2")).toEqual(new Set(["value2"]));
});

it("should include fields inside allowlisted fenced code blocks", () => {
const content = `
Id:: 343434

\`\`\`ad-note
Id:: 121212
\`\`\`

\`\`\`js
Id:: 999999
\`\`\`
`;
const result = InlineFieldParser.parseInlineFields(content, {
includeCodeBlocks: ["ad-note"],
});

expect(result.get("Id")).toEqual(new Set(["343434", "121212"]));
});

it("should match allowlisted fenced code block types case-insensitively", () => {
const content = `
\`\`\`Ad-Note title="Meta data"
Id:: 121212
\`\`\`
`;
const result = InlineFieldParser.parseInlineFields(content, {
includeCodeBlocks: ["ad-note"],
});

expect(result.get("Id")).toEqual(new Set(["121212"]));
});

it("should ignore fields in frontmatter", () => {
const content = `---
frontmatter:: ignored
Expand Down Expand Up @@ -108,8 +140,7 @@ regular:: this should be parsed
});

it("should handle field names with numbers and hyphens", () => {
const content =
"field-1:: value1\nfield_2:: value2\nfield3:: value3";
const content = "field-1:: value1\nfield_2:: value2\nfield3:: value3";
const result = InlineFieldParser.parseInlineFields(content);

expect(result.get("field-1")).toEqual(new Set(["value1"]));
Expand All @@ -128,10 +159,7 @@ regular:: this should be parsed

it("should return empty set for non-existent field", () => {
const content = "status:: active";
const result = InlineFieldParser.getFieldValues(
content,
"nonexistent",
);
const result = InlineFieldParser.getFieldValues(content, "nonexistent");

expect(result).toEqual(new Set());
});
Expand Down Expand Up @@ -166,5 +194,22 @@ regular:: this should be parsed
const result = InlineFieldParser.getFieldValues(content, "status");
expect(result).toEqual(new Set(["complete"]));
});

it("should only include allowlisted fenced code block values", () => {
const content = `
Id:: 343434
\`\`\`ad-note
Id:: 121212
\`\`\`
\`\`\`js
Id:: 999999
\`\`\`
`;
const result = InlineFieldParser.getFieldValues(content, "Id", {
includeCodeBlocks: ["ad-note"],
});

expect(result).toEqual(new Set(["343434", "121212"]));
});
});
});
});
Loading