Action

Debmundo MD to TP for OF v3

Posted by sylumer, Last update about 2 years ago - Unlisted

Steps

  • script

    //Take a backup
    draft.saveVersion();
    
    //Initialise
    let strIndent = "";
    
    //Strip blank lines (aggressive to catch multiples)
    draft.content = draft.content.replace(/(\n+)\n/g, "\n");
    
    //Convert title to Taskpaper project if it was not a heading
    //This caters for the original example which differs from the follow up real world example
    if (!draft.title.startsWith("#")) draft.content = draft.displayTitle + ":\n" + draft.processTemplate(`[[line|2..]]`);
    
    //Process the content we have at this point line by line
    let astrOut = draft.lines.map(strLine => 
    {
    
    	//Process lines that are level 3 headings
    	if(strLine.startsWith("###"))
    	{
    		strIndent = "\t\t\t";
    		return "\t\t- "+ strLine.slice(3).trim();
    	}
    
    	//Process lines that are level 2 headings
    	if(strLine.startsWith("##"))
    	{
    		strIndent = "\t\t";
    		return "\t- "+ strLine.slice(2).trim();
    	}
    
    	//Real world example had level 1 heading, so let's make thos projects
    	if(strLine.startsWith("#"))
    	{
    		strIndent = "\t";
    		return strLine.slice(1).trim() + ":";
    	}
    
    	//Process lines that are bulleted
    	if(strLine.trim().startsWith("- "))
    	{
    		//Build the output for a task
    		//Start with the indent level and the line content
    		let astrOutput = [strIndent + strLine.trim()];
    
    		//Create a regular expression to match some common URL types
            let reURL = new RegExp("(^|[ \t])((ftp|http|https|mailto|telnet|file):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))","g");
            
    		//Populate an array with URLs from the task line
    		let astrURL = [];
    		astrURL = astrURL.concat(strLine.replaceAll(/(\(|\))/g, " $1 ").match(reURL));
    
    		//If we have any URLs, concatenate them to the task as note entries
    		if (astrURL.length > 0) astrOutput = astrOutput.concat(astrURL);
    
    		//Return the task along with any notes
    		return astrOutput.join("\n");
    	}
    
    	//Ignore lines that would otherwise be notes (i.e. no leading markup)
    	return;
    });
    
    //Rebuild the processed content
    draft.content = astrOut.join("\n");
    
    //Strip Markdown for defined hyperlinks of the format "[]()"
    //Moved down here from the start as we need Markdown URLs in place to grab for the notes
    draft.content = draft.content.replaceAll(/\[(.*?)\]\((.*?)\)/g, "$1");
    
    //Because we are now stripping content lines that would otherwise have been notes, we need to strip blank lines again.
    draft.content = draft.content.replace(/(\n+)\n/g, "\n");
    
    //Be a good Drafts developer and explicitly force a draft update
    draft.update();

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.