Skip to content

Commit b1fad75

Browse files
committed
Added docstrings
1 parent 72257f2 commit b1fad75

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/compiler/plugins/AutoCardLinkCompiler.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { autoCardLink } from "src/utils/styles";
55
import Logger from "js-logger";
66
import { AUTO_CARD_LINK_PLUGIN_ID } from "src/ui/suggest/constants";
77

8+
/**
9+
* AutoCardLinkCompiler is responsible for compiling Auto Card Link queries
10+
* in the text of a PublishFile.
11+
* It replaces the queries with their rendered results and injects the necessary CSS
12+
* for the Auto Card Link renders.
13+
*
14+
* Documentation: {@link https://github.com/nekoshita/obsidian-auto-card-link}
15+
*/
816
export class AutoCardLinkCompiler {
917
app: App;
1018
serializer: XMLSerializer;
@@ -14,6 +22,14 @@ export class AutoCardLinkCompiler {
1422
this.serializer = new XMLSerializer();
1523
}
1624

25+
/**
26+
* Compiles the text by replacing Auto Card Link queries with their results.
27+
* It also injects the necessary CSS for the Auto Card Link renders.
28+
*
29+
* @param _file - The PublishFile object representing the file being compiled.
30+
* @returns A function that takes the text to compile and returns the compiled text.
31+
* @throws If the Auto Card Link plugin is not enabled, it returns the original text.
32+
*/
1733
compile: TCompilerStep = (_file) => async (text) => {
1834
let replacedText = text;
1935

@@ -62,6 +78,13 @@ export class AutoCardLinkCompiler {
6278
return replacedText + injectCSS;
6379
};
6480

81+
/**
82+
* Attempts to render a card link from the provided source string.
83+
* It parses the YAML metadata and generates the corresponding HTML element.
84+
*
85+
* @param source - The source string containing the YAML metadata for the card link.
86+
* @returns A Promise that resolves to an HTMLElement containing the rendered card link.
87+
*/
6588
async tryRenderCardLink(source: string) {
6689
const div = createEl("div");
6790

@@ -90,6 +113,15 @@ export class AutoCardLinkCompiler {
90113
return div;
91114
}
92115

116+
/**
117+
* Parses the YAML metadata from the provided source string and returns a LinkMetadata object.
118+
* It extracts the required fields (url, title) and optional fields (description, host, favicon, image).
119+
*
120+
* @param source - The source string containing the YAML metadata.
121+
* @returns A LinkMetadata object containing the parsed data.
122+
* @throws YamlParseError if the YAML parsing fails.
123+
* @throws NoRequiredParamsError if required parameters are missing.
124+
*/
93125
private parseLinkMetadataFromYaml(source: string): LinkMetadata {
94126
let yaml: Partial<LinkMetadata>;
95127

@@ -136,6 +168,13 @@ export class AutoCardLinkCompiler {
136168
};
137169
}
138170

171+
/**
172+
* Generates an error element with the provided error message.
173+
* This element is used to display errors related to card links.
174+
*
175+
* @param errorMsg - The error message to display.
176+
* @returns An HTMLElement containing the error message.
177+
*/
139178
private genErrorEl(errorMsg: string): HTMLElement {
140179
const containerEl = createEl("div");
141180
containerEl.addClass("auto-card-link-error-container");
@@ -147,6 +186,13 @@ export class AutoCardLinkCompiler {
147186
return containerEl;
148187
}
149188

189+
/**
190+
* Generates an HTML element representing a card link based on the provided LinkMetadata.
191+
* This element includes the title, description, host, and image (if available).
192+
*
193+
* @param data - The LinkMetadata object containing the link information.
194+
* @returns An HTMLElement representing the card link.
195+
*/
150196
private genLinkEl(data: LinkMetadata): HTMLElement {
151197
const containerEl = createEl("div");
152198
containerEl.addClass("auto-card-link-container");
@@ -197,6 +243,13 @@ export class AutoCardLinkCompiler {
197243
return containerEl;
198244
}
199245

246+
/**
247+
* Retrieves the local image path from the provided link.
248+
* It removes the surrounding brackets and resolves the link to a local path.
249+
*
250+
* @param link - The link to the image, which may be a local Obsidian link.
251+
* @returns The resolved local image path.
252+
*/
200253
private getLocalImagePath(link: string): string {
201254
link = link.slice(2, -2); // remove [[]]
202255

0 commit comments

Comments
 (0)