generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
90 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,40 @@ | ||
import { CachedMetadataPost, frontmatterSchema } from "."; | ||
import { App, TFile } from "obsidian"; | ||
export class PostValidationError extends Error { | ||
tFile: TFile; | ||
constructor(message: string, tFile: TFile) { | ||
super(message); | ||
this.tFile = tFile; | ||
} | ||
} | ||
|
||
function formatZodError( | ||
error: NonNullable< | ||
ReturnType<(typeof frontmatterSchema)["safeParse"]>["error"] | ||
>, | ||
) { | ||
const formattedError = error.issues.reduce((acc, issue) => { | ||
const message = `"${issue.path.join(".")}": ${issue.message}`; | ||
return acc + message + "\n"; | ||
}, "\n"); | ||
return formattedError; | ||
} | ||
|
||
export function validateMeta( | ||
file: TFile, | ||
ctx: { | ||
getFileMetadata: App["metadataCache"]["getFileCache"]; | ||
}, | ||
): CachedMetadataPost | PostValidationError { | ||
if (file.extension !== "md") | ||
return new PostValidationError("Not a markdown file", file); | ||
|
||
const meta = ctx.getFileMetadata(file); | ||
if (!meta || !meta.frontmatter || !meta.frontmatterPosition) | ||
return new PostValidationError("No frontmatter", file); | ||
|
||
const { error } = frontmatterSchema.safeParse(meta.frontmatter); | ||
if (error) return new PostValidationError(formatZodError(error), file); | ||
|
||
return meta as CachedMetadataPost; | ||
} |
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 |
---|---|---|
@@ -1,30 +1,2 @@ | ||
import { App, CachedMetadata, TFile, Notice } from "obsidian"; | ||
import type { SSSettings } from "@/Settings"; | ||
|
||
export interface Entry { | ||
tFile: TFile; | ||
content: string; | ||
meta: CachedMetadata | null; | ||
} | ||
|
||
export interface TransformCtx { | ||
cachedRead: App["vault"]["cachedRead"]; | ||
readBinary: App["vault"]["readBinary"]; | ||
getFileMetadata: App["metadataCache"]["getFileCache"]; | ||
resolveLink: App["metadataCache"]["getFirstLinkpathDest"]; | ||
notice: (...args: ConstructorParameters<typeof Notice>) => void; | ||
settings: SSSettings; | ||
} | ||
|
||
type CachedMetadataPost = Omit< | ||
CachedMetadata, | ||
"frontmatter" | "frontmatterPosition" | ||
> & { | ||
frontmatter: NonNullable<CachedMetadata["frontmatter"]>; | ||
frontmatterPosition: NonNullable<CachedMetadata["frontmatterPosition"]>; | ||
}; | ||
export interface Post extends Entry { | ||
meta: CachedMetadataPost; | ||
} | ||
|
||
export type { SSSettings } from "./Settings"; | ||
export type { Post } from "./transform"; |
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