Obsidian-like behaviour

So here’s where we are:

GV1

I’ve softened the colouring and added a new one: Green for external links.

You might guess that “Beeb News” and “BBC News” point to the same URL. The question is whether to detect such potentially misformed duplications and colour them yellow. It could be legitimate to have two sets of words for the same external link or it could be unintended inconsistency.

I’ve also - thanks to @sylumer - changed the code so that it processes all the drafts in the current workspace. It also now writes to the clipboard.

Anyhow here’s the code as it currently stands:

// Takes the drafts from the current workspace and makes a
// GraphViz .dot file on the clipboard.

// Get an array of the drafts in the current workspace
drafts = app.currentWorkspace.query("inbox")

// Process each draft to get its title and the titles of the drafts it links to
draftTitles = []
draftInternalLinks = []
draftExternalLinks = []
foundExternalLinks = []

//internalLinkRegex = /\[\[(.*?)\]\]/g
internalLinkRegex = /\[\[.*?\]\]/g

//externalLinkRegex = /\[(.*?)\]\((.*?)\)/g
externalLinkRegex = /\[.*?\]\(.*?\)/g

// Iterate over the drafts in this workspace
for(i = 0 ; i < drafts.length ; i++){
    theDraft = drafts[i]
    draftTitles[i] = theDraft.title

    // Get internal links - if any
    matches = theDraft.content.match(internalLinkRegex)
    if (matches != null){
        draftInternalLinks[i] = matches.join("\n").replaceAll("[","").replaceAll("]","").split("\n")
    }else{
        draftInternalLinks[i] = []
    }

    // Get external links - if any
    matches = theDraft.content.match(externalLinkRegex)
    if (matches != null){
        draftExternalLinks[i] = matches.join("\n").replaceAll("[","").replaceAll("]","").replaceAll("(","!!!XYZZY!!!").replaceAll(")","").split("\n")
    }else{
        draftExternalLinks[i] = []
    }
}

// Compose the .dot output
GraphViz = 'digraph {\n'
missingDrafts = []
for(i = 0 ; i < drafts.length ; i++){
    theDraft = drafts[i]
    draftURL = 'drafts5://open?uuid=' + theDraft.uuid
    // Write the node out
    GraphViz += '    N' + i.toString() + '[label="' + theDraft.title + '" ; URL = "' + draftURL + '"]\n'

    // Write each internal link out - if found
    for(j = 0; j < draftInternalLinks[i].length; j++){
        otherDraftName = draftInternalLinks[i][j]
        otherDraft = draftTitles.indexOf(otherDraftName)
        if(otherDraft > -1){
            // Have another draft to point at
            GraphViz += '        N' + i.toString() + ' -> N' + otherDraft.toString() +'\n'
        }else{
            // This is a missing draft
            missingDraftIndex = missingDrafts.indexOf(otherDraftName)
            if (missingDraftIndex == -1) {
                // Missing draft not already in the list
                missingDrafts.push(otherDraftName)
                missingDraftIndex = missingDrafts.length - 1
                // Write the node out
                GraphViz += '    M' + missingDraftIndex.toString() + '[fillcolor = lightcoral ; style = filled ; label="' + otherDraftName + '"]\n'
            }
        
            GraphViz += '        N' + i.toString() + ' -> M' + missingDraftIndex.toString() +' [color = lightcoral]\n'
        }
    }

    // Write each external link out - if found
    for(j = 0; j < draftExternalLinks[i].length; j++){
        if(foundExternalLinks.indexOf(draftExternalLinks[i][j]) == -1){
            // Register fresh external link name/URL combo
            foundExternalLinks.push(draftExternalLinks[i][j])
            linkNumber = foundExternalLinks.length - 1
        
            // Create node for external name/URL combo
            splitLink = draftExternalLinks[i][j].split("!!!XYZZY!!!")
            otherPageName = splitLink[0]
            otherPageURL = splitLink[1]
            GraphViz += '    E' + linkNumber.toString() + '[label = "' + otherPageName + '"; fillcolor = darkseagreen2; style = filled; URL="' + otherPageURL + '" ]' + '\n'
        }
            GraphViz += '        N' + i.toString() + ' -> E' + linkNumber.toString() +' [color = darkseagreen2]\n'
    }
}
GraphViz += '}\n'
app.setClipboard(GraphViz)

And here’s the GraphViz .dot output:

digraph {
    N0[label="Draft D" ; URL = "drafts5://open?uuid=CAC17EE9-1DBF-42D6-82F7-4D1A827B7D1C"]
        N0 -> N3
        N0 -> N2
        N0 -> N4
    E0[label = "Beeb News"; fillcolor = darkseagreen2; style = filled; URL="https://www.bbc.co.uk/news" ]
        N0 -> E0 [color = darkseagreen2]
    N1[label="Draft E" ; URL = "drafts5://open?uuid=ED8459D8-C0C6-4ECF-9BD0-F0CFABCE7DD0"]
    N2[label="Draft B" ; URL = "drafts5://open?uuid=114B9649-42BC-40DE-AC3B-823180A1867B"]
        N2 -> N3
    E1[label = "IBM"; fillcolor = darkseagreen2; style = filled; URL="https://www.ibm.com" ]
        N2 -> E1 [color = darkseagreen2]
    E2[label = "BBC News"; fillcolor = darkseagreen2; style = filled; URL="https://www.bbc.co.uk/news" ]
        N2 -> E2 [color = darkseagreen2]
    N3[label="Draft A" ; URL = "drafts5://open?uuid=07CE3469-17F0-4114-BB2F-C88329F9C4EA"]
        N3 -> N2
        N3 -> N4
    M0[fillcolor = lightcoral ; style = filled ; label="Draft Zero"]
        N3 -> M0 [color = lightcoral]
    N4[label="Draft C" ; URL = "drafts5://open?uuid=BB986330-D24B-453C-9B75-7ECDC2FB925A"]
        N4 -> N0
}
3 Likes