Action

Graphing wiki-links (early experiment)

Posted by @jsamlarose, Last update over 3 years ago - Unlisted

UPDATES

over 3 years ago

Minor updates:
- trap error when draft contains no wiki-links
- trap error when draft contains multiple instances of the same wiki-link

A sketch of something that might be useful when it grows up…

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    
    
    var links = draft.content.match(/\[\[[\#|\w|\s|@|\.|\?|»|\:|\(|\)|\/|\||\-|\+"]+\]\]/g)
    
    if (Array.isArray(links) && links.length) {
    	
    	var checker = value =>
    	  !["[[sx:", "[[sc:", "[[ax:", "[[s:", "[[url:","[[map:"].some(element => value.includes(element));
    	
    	var edges = "";
    	links.filter(checker).forEach(edgeFunction);
    	function edgeFunction(value, index, array) {
    	  edges += "{from: '[[" + draft.displayTitle + "]]', to: '" + value + "'}\n"
    	}
    	edges = edges.replace(/\}\n\{/g, "\},\n\{")
    	
    	draft.setTemplateTag("edges",edges)
    	
    	links[0] = "[[" + draft.displayTitle + "]]"
    	
    	var nodes = "";
    	links = [...new Set(links)];
    	links.filter(checker).forEach(myFunction);
    	
    	function myFunction(value, index, array) {
    	  nodes += "{id: '" + value + "', label: '" + value.split('[[').pop().split(']]')[0].replace("# ","") + "'}\n"
    	}
    	
    	nodes = nodes.replace(/\}\n\{/g, "\},\n\{")
    	draft.setTemplateTag("nodes",nodes)
    
    } else { 
    
    app.displayInfoMessage("Hm. Can't see any wiki-links in this draft. Try another?")
    	context.cancel()
    	
    	} 
    // alert(edges)
    
  • htmlpreview

    <html>
    <head>
        <script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
    
        <style type="text/css">
            #mynetwork {
                width: 100%;
                height: 100%;
                /* border: 1px solid lightgray; */
            }
        </style>
    </head>
    <body>
    <div id="mynetwork"></div>
    
    <script type="text/javascript">
        // create an array with nodes
        var nodes = new vis.DataSet([
            [[nodes]]
        ]);
    
        // create an array with edges
        var edges = new vis.DataSet([
            [[edges]]
        ]);
    
        // create a network
        var container = document.getElementById('mynetwork');
    
        // provide the data in the vis format
        var data = {
            nodes: nodes,
            edges: edges
        };
        var options = {};
    
        // initialize your network!
        var network = new vis.Network(container, data, options);
    </script>
    </body>
    </html>
    
  • clipboard

    template
    
    
        
    
        
    
    
    

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.