mdelobelle / metadatamenu

For data management enthusiasts : type and manage the metadata of your notes.
https://mdelobelle.github.io/metadatamenu
MIT License
477 stars 27 forks source link

Lookup Field Type Can't Find Links in HTML Formatted Values #689

Open LynetteCullens opened 4 weeks ago

LynetteCullens commented 4 weeks ago

Property Formatted in Frontmatter

LinkedDimensions: <span class="center-align"><sup style="font-size:150%;"><span style="font-size:200%;">[</span><span class="frac"><sup>[[Length#^Dimensions]]</sup><span>/</span><sub>[[Time#^Dimensions]]<sup>2</sup></sub></span><span style="font-size:200%;">]</span></span></span>

Lookup Query

image

I cannot for the life of me gather links inside text with HTML format in my frontmatter. I've resorted to using a dv.view to regex the links. I've tried various ways to add a regex expression within the Lookup Modal. Nothing works.

Dataviewjs That Works

(async () => {
    const filePath = dv.current().file.path;
    const namedFileFieldsData = await MetadataMenu.api.namedFileFields(filePath);

    if (!namedFileFieldsData) {
        console.error('Error fetching named file fields');
        return;
    }

    const dimensionsOutput = [];

    // Modify indexed paths and values
    Object.entries(namedFileFieldsData).forEach(([indexedPath, fieldInfo]) => {
        if (indexedPath === "mathLink-blocks____LinkedDimensions" && fieldInfo.value) {
            // Apply regex replace to extract constant names from the value field
            const constants = fieldInfo.value.match(/\[\[([^\]]+)\]\]/g);
            if (constants) {
                constants.forEach(constant => {
                    // Extract the constant name from the matched string
                    const constantName = constant.substring(2, constant.length - 2);
                    dimensionsOutput.push(`[[${constantName}]]`);
                });
            }
        }
    });

    // Output dimensions as a list
    dv.list(dimensionsOutput.map(item => `"${item}"`));
})();

dv.view that works

// Wrapping the code in an async function to use await
(async () => {
    const { update } = app.plugins.plugins["metaedit"].api;
    const activeFile = app.workspace.getActiveFile();
    const currentTitle = activeFile ? activeFile.basename : "";

    // Define fieldsToCheck array as optional, and filter out any potential undefined elements
    const fieldsToCheck = [
        "LinkedDimensions"
    ].filter(field => field);

    let links = [];

    // Check if fieldsToCheck is defined before iterating over it
    if (fieldsToCheck) {
        fieldsToCheck.forEach(field => {
            // Use optional chaining to safely access nested properties
            const fieldContent = dv.current().file.frontmatter["mathLink-blocks"]?.[field];
            if (fieldContent) {
                const matches = fieldContent.match(/\[\[([^\]]+)\]\]/g);
                if (matches) links.push(...matches);
            }
        });
    }

    links = [...new Set(links)].filter(link => link !== '[[Variables]]');

    // Filter out links that match the current file's title
    links = links.filter(link => !link.includes(currentTitle));

    // Update the property in the document's frontmatter only if there are links
    if (links.length > 0) {
        // Update the GetLinks property in the document's frontmatter
        const currentFrontmatter = dv.current().file.frontmatter;
        currentFrontmatter.dimensions = links;

        // Check if any of the specified fields exist before updating
        if (fieldsToCheck && Object.keys(currentFrontmatter["mathLink-blocks"] || {}).some(field => fieldsToCheck.includes(field))) {
            await update('dimensions', links, activeFile);
            console.log('Frontmatter updated successfully');
        }
    } else {
        console.log('No links to update');
    }

    // Display the links in the view
    dv.paragraph(`Found links: ${links.join(", ")}`);
})();