Skip to content
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

feat: allow custom before/after in pages #27

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,27 @@ export default class TagPagePlugin extends Plugin {
tagOfInterest,
);

if (!activeLeaf.file) {
return;
}

this.app.fileManager.processFrontMatter(activeLeaf.file, frontMatter => {
frontMatter[this.settings.frontmatterQueryProperty] = tagOfInterest
frontMatter.tags ??= [];
frontMatter.tags = [...new Set(frontMatter.tags).add('tag-page-md').add(tagOfInterest.slice(1) /* Omit the leading # */)]
})

const baseContent = await this.app.vault.read(activeLeaf.file);
const tagPageContentString = await generateTagPageContent(
this.app,
this.settings,
tagsInfo,
tagOfInterest,
baseContent,
);

swapPageContent(activeLeaf, tagPageContentString);
activeLeaf.save();
}

/**
Expand Down
45 changes: 40 additions & 5 deletions src/utils/pageContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,64 @@ import { getIsWildCard } from './tagSearch';
* @param {PluginSettings} settings - The plugin settings.
* @param {TagInfo[]} tagsInfo - Information about tags.
* @param {string} tagOfInterest - The tag for which the page is being generated.
* @param {string} baseContent - The original content of the page
* @returns {Promise<string>} - The content to be set in the tag page.
*/
export type GenerateTagPageContentFn = (
app: App,
settings: PluginSettings,
tagsInfo: TagInfo,
tagOfInterest: string,
baseContent?: string,
) => Promise<string>;

const _parseContent = (
baseContent: string,
) => {
const match = baseContent.match(
/^(?<frontmatter>---\n.*?\n---\n)?(?:(?<before>.*?)\n)?(?<tagpage>%%\ntag-page-md.*?tag-page-md end\n%%)(?:\n(?<after>.*?))?$/s,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frontmatter & tagpage groups do not need to be captured, but I used them as a way to comment parts of the pattern

);
if (!match || !match.groups) {
return { frontmatter: '', before: '', after: '' };
}
return {
frontmatter: match.groups.frontmatter ?? '',
before: match.groups.before ?? '',
after: match.groups.after ?? '',
};
};
/**
* Generates the content for a tag page.
*
* @param {App} app - The Obsidian App instance.
* @param {PluginSettings} settings - The plugin settings.
* @param {TagInfo[]} tagsInfo - Information about tags.
* @param {string} tagOfInterest - The tag for which the page is being generated.
* @param {string} baseContent - The original content of the page
* @returns {Promise<string>} - The content to be set in the tag page.
*/
export const generateTagPageContent: GenerateTagPageContentFn = async (
app: App,
settings: PluginSettings,
tagsInfo: TagInfo,
tagOfInterest: string,
baseContent = '',
): Promise<string> => {
// Generate list of links to files with this tag
const tagPageContent: string[] = [];
tagPageContent.push(
`---\n${settings.frontmatterQueryProperty}: "${tagOfInterest}"\n---`,
);

// Try to extract comments from the page to spot injection placeholder
const { frontmatter, before, after } = _parseContent(baseContent);

if(frontmatter){
tagPageContent.push(frontmatter);
}

if (before) {
tagPageContent.push(before);
}
tagPageContent.push('%%\ntag-page-md\n%%\n');

tagPageContent.push(`## Tag Content for ${tagOfInterest.replace('*', '')}`);

// Check if we have more than one baseTag across all tagInfos
Expand Down Expand Up @@ -85,6 +114,11 @@ export const generateTagPageContent: GenerateTagPageContentFn = async (
tagPageContent.push(`## Files with ${cleanedTag} in frontmatter`);
tagPageContent.push(...filesWithFrontmatterTag);
}

tagPageContent.push('\n%%\ntag-page-md end\n%%');
if (after) {
tagPageContent.push(after);
}
return tagPageContent.join('\n');
};

Expand All @@ -103,8 +137,9 @@ export const extractFrontMatterTagValue = (
): string | undefined => {
if (view.file) {
try {
const metaMatter = app.metadataCache.getFileCache(view.file)
?.frontmatter;
const metaMatter = app.metadataCache.getFileCache(
view.file,
)?.frontmatter;

return metaMatter?.[frontMatterTag];
} catch (err) {
Expand Down