Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save GitMurf/ec35c81003b172ce01044993d9849505 to your computer and use it in GitHub Desktop.

Select an option

Save GitMurf/ec35c81003b172ce01044993d9849505 to your computer and use it in GitHub Desktop.

Revisions

  1. GitMurf revised this gist Feb 13, 2022. 1 changed file with 10 additions and 6 deletions.
    16 changes: 10 additions & 6 deletions obsidian.templater.clean.selection.for-external-pasting.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ if(editor.somethingSelected() === true) {
    let codeBlock = false;
    splitLines.forEach(eachLine => {
    let lnOutput = eachLine;
    const listMatch = lnOutput.match(/^[ \t]*[\-\*] /)
    const listMatch = lnOutput.match(/^[ \t]*[\-\*] /);
    if(listItem === false) {
    if(listMatch) {
    listItem = true;
    @@ -26,16 +26,20 @@ if(editor.somethingSelected() === true) {
    lnOutput = lnOutput.replace(/#[^\s\#]+/g, '');
    //replace a [ ] task checkbox
    lnOutput = lnOutput.replace(/\[ \]/g, '<span style="background-color: #FFFF00"><strong>ACTION ITEM:</strong></span>');
    //Parse out alias links
    //Parse out alias from wiki links
    lnOutput = lnOutput.replace(/\[\[[^\[\]]+\|([^\[\]]+)\]\]/g, '$1');
    //Parse out regular bracket links
    //Parse out regular wiki bracket links
    lnOutput = lnOutput.replace(/\[\[([^\[\]]+)\]\]/g, '$1');
    //Parse out markdown style URLs
    lnOutput = lnOutput.replace(/\[[^\[\]]*\]\(([^ ]+)\)/g, '$1');
    //Bold
    lnOutput = lnOutput.replace(/\[([^\[\]]*)\]\(([^ ]+)\)/g, '<a href="$2">$1</a>');
    //Bold asterisk
    lnOutput = lnOutput.replace(/\*\*([^\*]+)\*\*/g, '<strong>$1</strong>');
    //Italics
    //Bold underscore
    lnOutput = lnOutput.replace(/\_\_([^\_]+)\_\_/g, '<strong>$1</strong>');
    //Italics asterisk
    lnOutput = lnOutput.replace(/\*([^\*]+)\*/g, '<em>$1</em>');
    //Italics underscore
    lnOutput = lnOutput.replace(/\_([^\_]+)\_/g, '<em>$1</em>');
    //Highlights
    lnOutput = lnOutput.replace(/\=\=([^\=]+)\=\=/g, '<span style="background-color: #FFFF00">$1</span>');
    //Strikethrough
  2. GitMurf revised this gist Feb 12, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion obsidian.templater.clean.selection.for-external-pasting.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ if(editor.somethingSelected() === true) {
    let codeBlock = false;
    splitLines.forEach(eachLine => {
    let lnOutput = eachLine;
    const listMatch = lnOutput.match(/^[ ]*[\-\*] /)
    const listMatch = lnOutput.match(/^[ \t]*[\-\*] /)
    if(listItem === false) {
    if(listMatch) {
    listItem = true;
  3. GitMurf revised this gist Feb 12, 2022. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions obsidian.templater.clean.selection.for-external-pasting.js
    Original file line number Diff line number Diff line change
    @@ -80,11 +80,11 @@ if(editor.somethingSelected() === true) {

    //Formatting for paste into tools like Microsoft Word, OneNote, Outlook to match their bullet styles
    lnOutput = lnOutput.replace(/^[\-\*] (.*)/, '<li>$1</li>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><li>$1</li></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><li>$1</li></ul></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><ul><li>$1</li></ul></ul></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><ul><ul><li>$1</li></ul></ul></ul></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><ul><ul><ul><li>$1</li></ul></ul></ul></ul></ul>');
    lnOutput = lnOutput.replace(/^( |\t)[\-\*] (.*)/, '<ul><li>$2</li></ul>');
    lnOutput = lnOutput.replace(/^( |\t\t)[\-\*] (.*)/, '<ul><ul><li>$2</li></ul></ul>');
    lnOutput = lnOutput.replace(/^( |\t\t\t)[\-\*] (.*)/, '<ul><ul><ul><li>$2</li></ul></ul></ul>');
    lnOutput = lnOutput.replace(/^( |\t\t\t\t)[\-\*] (.*)/, '<ul><ul><ul><ul><li>$2</li></ul></ul></ul></ul>');
    lnOutput = lnOutput.replace(/^( |\t\t\t\t\t)[\-\*] (.*)/, '<ul><ul><ul><ul><ul><li>$2</li></ul></ul></ul></ul></ul>');

    if(listItem === false) {
    finalOutput = `${finalOutput}<br>${lnOutput}`
  4. GitMurf revised this gist Feb 12, 2022. 1 changed file with 54 additions and 6 deletions.
    60 changes: 54 additions & 6 deletions obsidian.templater.clean.selection.for-external-pasting.js
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ if(editor.somethingSelected() === true) {
    let splitLines = selectedText.split('\n');
    let finalOutput = '';
    let listItem = false;
    let codeBlock = false;
    splitLines.forEach(eachLine => {
    let lnOutput = eachLine;
    const listMatch = lnOutput.match(/^[ ]*[\-\*] /)
    @@ -22,15 +23,58 @@ if(editor.somethingSelected() === true) {
    }

    //remove tags
    lnOutput = lnOutput.replace(/#\S*/g, '');
    lnOutput = lnOutput.replace(/#[^\s\#]+/g, '');
    //replace a [ ] task checkbox
    lnOutput = lnOutput.replace(/\[ \]/g, '*ACTION ITEM*:');
    lnOutput = lnOutput.replace(/\[ \]/g, '<span style="background-color: #FFFF00"><strong>ACTION ITEM:</strong></span>');
    //Parse out alias links
    lnOutput = lnOutput.replace(/\[\[[^\[\]]+\|([^\[\]]+)\]\]/g, '$1');
    //Parse out regular bracket links
    lnOutput = lnOutput.replace(/\[\[([^\[\]]+)\]\]/g, '$1');
    //Parse out markdown style URLs
    lnOutput = lnOutput.replace(/\[[^\[\]]*\]\(([^ ]+)\)/g, '$1');
    //Bold
    lnOutput = lnOutput.replace(/\*\*([^\*]+)\*\*/g, '<strong>$1</strong>');
    //Italics
    lnOutput = lnOutput.replace(/\*([^\*]+)\*/g, '<em>$1</em>');
    //Highlights
    lnOutput = lnOutput.replace(/\=\=([^\=]+)\=\=/g, '<span style="background-color: #FFFF00">$1</span>');
    //Strikethrough
    lnOutput = lnOutput.replace(/\~\~([^\~]+)\~\~/g, '<strike>$1</strike>');

    //Inline code
    let backTickChar = "`";
    let regExpStr = `${backTickChar}+([^${backTickChar}]+)${backTickChar}+`;
    let regReplace = new RegExp(regExpStr, "g");
    lnOutput = lnOutput.replace(regReplace, '<code>$1</code>');

    //Full code blocks
    if(lnOutput.indexOf("```") > -1) {
    let backTickChar = "`";
    let regExpStr = `${backTickChar}+[^${backTickChar}]*`;
    let regReplace = new RegExp(regExpStr, "g");
    if(codeBlock === false) {
    codeBlock = true;
    lnOutput = lnOutput.replace(regReplace, '<code>');
    } else {
    codeBlock = false;
    lnOutput = lnOutput.replace(regReplace, '</code>');
    }
    }

    //HEADERS
    //H1
    lnOutput = lnOutput.replace(/^\# (.*)/, '<h1>$1</h1>');
    //H2
    lnOutput = lnOutput.replace(/^\## (.*)/, '<h2>$1</h2>');
    //H3
    lnOutput = lnOutput.replace(/^\### (.*)/, '<h3>$1</h3>');
    //H4
    lnOutput = lnOutput.replace(/^\#### (.*)/, '<h4>$1</h4>');
    //H5
    lnOutput = lnOutput.replace(/^\##### (.*)/, '<h5>$1</h5>');
    //H6
    lnOutput = lnOutput.replace(/^\###### (.*)/, '<h6>$1</h6>');

    //Remove extra whitespace
    lnOutput = lnOutput.replace(/(\S)[ ]+/g, '$1 ');

    @@ -54,19 +98,23 @@ if(editor.somethingSelected() === true) {
    }
    let newString = finalOutput.trim();
    newString = newString.replace(/\<\/ul\>(\<br\>)+/g, '</ul>');
    newString = newString.replace(/\<\/h([123456])\>(\<br\>)+/g, '</h$1>');

    const plainText = await navigator.clipboard.readText();
    //This was keeping the previous clipboard item in the plaintext of the clipboard
    //No longer using this as kind of confusing
    //const plainText = await navigator.clipboard.readText();
    const plainText = newString;
    const blobPlain = new Blob([plainText], { type: "text/plain" });
    const blobHtml = new Blob([`${newString}`], { type: "text/html" });
    let data = [new ClipboardItem({
    [blobPlain.type]: blobPlain,
    [blobHtml.type]: blobHtml,
    })];
    await navigator.clipboard.write(data);
    new Notice("Copied the cleaned and formatted text to your Clipboard for pasting into Outlook / Word!", 10000);
    new Notice("Kept the previous Clipboard text stored as Plain Text with the new stuff at HTML!", 10000);
    new Notice("Copied the cleaned and formatted text to your Clipboard for pasting into Outlook / Word!", 15000);
    new Notice("Make sure you do a regular 'Full' paste and NOT paste as plain text!", 15000);
    } else {
    new Notice("Doing NOTHING because no text was selected!", 10000);
    new Notice("Doing NOTHING because no text was selected!", 15000);
    }

    //By doing a return then it will not try to replace the selected text / apply the tR variable
  5. GitMurf revised this gist Feb 11, 2022. 1 changed file with 11 additions and 3 deletions.
    14 changes: 11 additions & 3 deletions obsidian.templater.clean.selection.for-external-pasting.js
    Original file line number Diff line number Diff line change
    @@ -21,14 +21,17 @@ if(editor.somethingSelected() === true) {
    }
    }

    //remove tags
    lnOutput = lnOutput.replace(/#\S*/g, '');
    //replace a [ ] task checkbox
    lnOutput = lnOutput.replace(/\[ \]/g, '*ACTION ITEM*:');
    //Parse out alias links
    lnOutput = lnOutput.replace(/\[\[[^\[\]]+\|([^\[\]]+)\]\]/g, '$1');
    //Parse out regular bracket links
    lnOutput = lnOutput.replace(/\[\[([^\[\]]+)\]\]/g, '$1');
    //Parse out markdown style URLs
    lnOutput = lnOutput.replace(/\[[^\[\]]*\]\(([^ ]+)\)/g, '$1');
    //Remove extra whitespace
    lnOutput = lnOutput.replace(/(\S)[ ]+/g, '$1 ');

    //Formatting for paste into tools like Microsoft Word, OneNote, Outlook to match their bullet styles
    @@ -38,14 +41,19 @@ if(editor.somethingSelected() === true) {
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><ul><li>$1</li></ul></ul></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><ul><ul><li>$1</li></ul></ul></ul></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><ul><ul><ul><li>$1</li></ul></ul></ul></ul></ul>');

    finalOutput = `${finalOutput}\n${lnOutput}`;

    if(listItem === false) {
    finalOutput = `${finalOutput}<br>${lnOutput}`
    } else {
    finalOutput = `${finalOutput}${lnOutput}`
    }
    })

    if(listItem === true) {
    finalOutput = `${finalOutput}</ul>`;
    }
    const newString = finalOutput.trim();
    let newString = finalOutput.trim();
    newString = newString.replace(/\<\/ul\>(\<br\>)+/g, '</ul>');

    const plainText = await navigator.clipboard.readText();
    const blobPlain = new Blob([plainText], { type: "text/plain" });
  6. GitMurf created this gist Feb 11, 2022.
    66 changes: 66 additions & 0 deletions obsidian.templater.clean.selection.for-external-pasting.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    <%*
    const editor = app.workspace.activeLeaf.view.editor;

    if(editor.somethingSelected() === true) {
    let selectedText = editor.getSelection();
    let splitLines = selectedText.split('\n');
    let finalOutput = '';
    let listItem = false;
    splitLines.forEach(eachLine => {
    let lnOutput = eachLine;
    const listMatch = lnOutput.match(/^[ ]*[\-\*] /)
    if(listItem === false) {
    if(listMatch) {
    listItem = true;
    finalOutput = `${finalOutput}<ul>`;
    }
    } else {
    if(!listMatch) {
    listItem = false;
    finalOutput = `${finalOutput}</ul>`;
    }
    }

    lnOutput = lnOutput.replace(/#\S*/g, '');
    //replace a [ ] task checkbox
    lnOutput = lnOutput.replace(/\[ \]/g, '*ACTION ITEM*:');
    //Parse out alias links
    lnOutput = lnOutput.replace(/\[\[[^\[\]]+\|([^\[\]]+)\]\]/g, '$1');
    //Parse out regular bracket links
    lnOutput = lnOutput.replace(/\[\[([^\[\]]+)\]\]/g, '$1');
    lnOutput = lnOutput.replace(/\[[^\[\]]*\]\(([^ ]+)\)/g, '$1');
    lnOutput = lnOutput.replace(/(\S)[ ]+/g, '$1 ');

    //Formatting for paste into tools like Microsoft Word, OneNote, Outlook to match their bullet styles
    lnOutput = lnOutput.replace(/^[\-\*] (.*)/, '<li>$1</li>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><li>$1</li></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><li>$1</li></ul></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><ul><li>$1</li></ul></ul></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><ul><ul><li>$1</li></ul></ul></ul></ul>');
    lnOutput = lnOutput.replace(/^ [\-\*] (.*)/, '<ul><ul><ul><ul><ul><li>$1</li></ul></ul></ul></ul></ul>');

    finalOutput = `${finalOutput}\n${lnOutput}`;
    })

    if(listItem === true) {
    finalOutput = `${finalOutput}</ul>`;
    }
    const newString = finalOutput.trim();

    const plainText = await navigator.clipboard.readText();
    const blobPlain = new Blob([plainText], { type: "text/plain" });
    const blobHtml = new Blob([`${newString}`], { type: "text/html" });
    let data = [new ClipboardItem({
    [blobPlain.type]: blobPlain,
    [blobHtml.type]: blobHtml,
    })];
    await navigator.clipboard.write(data);
    new Notice("Copied the cleaned and formatted text to your Clipboard for pasting into Outlook / Word!", 10000);
    new Notice("Kept the previous Clipboard text stored as Plain Text with the new stuff at HTML!", 10000);
    } else {
    new Notice("Doing NOTHING because no text was selected!", 10000);
    }

    //By doing a return then it will not try to replace the selected text / apply the tR variable
    return;
    %>