Action

Transclude > New Draft

Posted by agiletortoise, Last update 16 days ago

Replace any [[Wiki-Style Links]] to other drafts with the content of the draft they are linked to and output the full content to a new draft.

Steps

  • script

    let findLinkedDraft = (title) => {
    	let found = Draft.query(title, "all", [], [], "modified", true, false);
    	if (found.length == 0) {
    		return null;
    	}
    	for (let d of found) {
    		if (d.content.trim().length > 0) {
    			let firstLine = d.content.trim().split("\n")[0];
    			if (firstLine.toLowerCase().includes(title.toLowerCase())) {
    				return d;
    			}
    		}
    	}
    }
    
    let transclude = (d) => {
    	let re = /\[\[(.+?)\]\]/gm;
    	let content = draft.content;
    
    	content = content.replace(re, (match, title) => {
    		let d = findLinkedDraft(title);
    		if (d) {
    			return d.content;
    		}
    		else { // not found, leave as-is
    			return match;
    		}
    	});
    	return content;
    }
    
    // expand links to their content and create new draft...
    let newDraft = new Draft();
    newDraft.content = transclude(draft);
    newDraft.update();
    editor.load(newDraft);

Options

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