obsidian#parseLinktext TypeScript Examples

The following examples show how to use obsidian#parseLinktext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: main.tsx    From obsidian-annotator with GNU Affero General Public License v3.0 6 votes vote down vote up
private addMarkdownPostProcessor() {
        const markdownPostProcessor = async (el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
            for (const link of el.getElementsByClassName('internal-link') as HTMLCollectionOf<HTMLAnchorElement>) {
                const href = link.getAttribute('href');
                const parsedLink = parseLinktext(href);
                const annotationid = parsedLink.subpath.startsWith('#^') ? parsedLink.subpath.substr(2) : null;
                const file = this.app.metadataCache.getFirstLinkpathDest(parsedLink.path, ctx.sourcePath);
                if (this.isAnnotationFile(file)) {
                    link.addEventListener('click', ev => {
                        ev.preventDefault();
                        ev.stopPropagation();
                        ev.stopImmediatePropagation();
                        const inNewPane = ev.metaKey || ev.ctrlKey || ev.button == 1;
                        this.openAnnotationTarget(file, inNewPane, annotationid);
                    });
                }
            }
        };

        this.registerMarkdownPostProcessor(markdownPostProcessor);
    }
Example #2
Source File: InternalModuleFile.ts    From Templater with GNU Affero General Public License v3.0 5 votes vote down vote up
generate_include(): (include_link: string | TFile) => Promise<string> {
        return async (include_link: string | TFile) => {
            // TODO: Add mutex for this, this may currently lead to a race condition.
            // While not very impactful, that could still be annoying.
            this.include_depth += 1;
            if (this.include_depth > DEPTH_LIMIT) {
                this.include_depth -= 1;
                throw new TemplaterError(
                    "Reached inclusion depth limit (max = 10)"
                );
            }

            let inc_file_content: string;

            if (include_link instanceof TFile) {
                inc_file_content = await this.app.vault.read(include_link);
            } else {
                let match;
                if ((match = this.linkpath_regex.exec(include_link)) === null) {
                    this.include_depth -= 1;
                    throw new TemplaterError(
                        "Invalid file format, provide an obsidian link between quotes."
                    );
                }
                const { path, subpath } = parseLinktext(match[1]);

                const inc_file = this.app.metadataCache.getFirstLinkpathDest(
                    path,
                    ""
                );
                if (!inc_file) {
                    this.include_depth -= 1;
                    throw new TemplaterError(
                        `File ${include_link} doesn't exist`
                    );
                }
                inc_file_content = await this.app.vault.read(inc_file);

                if (subpath) {
                    const cache = this.app.metadataCache.getFileCache(inc_file);
                    if (cache) {
                        const result = resolveSubpath(cache, subpath);
                        if (result) {
                            inc_file_content = inc_file_content.slice(
                                result.start.offset,
                                result.end?.offset
                            );
                        }
                    }
                }
            }

            try {
                const parsed_content =
                    await this.plugin.templater.parser.parse_commands(
                        inc_file_content,
                        this.plugin.templater.current_functions_object
                    );
                this.include_depth -= 1;
                return parsed_content;
            } catch (e) {
                this.include_depth -= 1;
                throw e;
            }
        };
    }