You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Advanced anchor texts (i.e. figure captions) can be messed up by the current version of "Remove Links" built--in, so I wrote my own.
In particular, if you have pandoc references [@Reference] or math with vertical bars $\left|M\right|$ you may benefit from this. It essentially removes the logic to cleanse vertical bars from link anchor text, checks for newlines and skips removal if it finds a matching pair of brackets but not on one line, and skips escaped brackets.
I used ChatGPT so it also adjusted the logic which makes it hard for me to suggest a 1:1 request to update the code as it's built in.
Details
module.exports={id: "remove-links",description: {name: "Remove Links",description: "Removes wiki and/or external links.",availableKinds: ["Scene","Manuscript"],options: [{id: "remove-wikilinks",name: "Remove Wikilinks",description: "Remove brackets from [[wikilinks]].",type: Boolean,default: true,},{id: "remove-external-links",name: "Remove External Links",description: "Remove external links, leaving only the anchor text.",type: Boolean,default: true,},],},compile(input,context){constremoveWikilinks=context.optionValues["remove-wikilinks"];constremoveExternalLinks=context.optionValues["remove-external-links"];constreplaceLinks=(contents)=>{if(removeWikilinks){contents=replaceWikiLinks(contents);}if(removeExternalLinks){contents=replaceExternalLinks(contents);}returncontents;};if(context.kind==="Scene"){return(input).map((sceneInput)=>{constcontents=replaceLinks(sceneInput.contents);return{
...sceneInput,
contents,};});}else{return{
...(input),contents: replaceLinks((input).contents),};}},};functionreplaceWikiLinks(contents){letstartOfAlias=-1;letadditionalAlias=false;letend=-1;// moving backward allows us to replace within the loop,// so no additional memory.for(leti=contents.length-1;i>=0;i--){constchar=contents.charAt(i);if(end<0){if(char==="]"&&i>0&&contents.charAt(i-1)==="]"){end=i-1;i-=1;}}elseif(char==="|"){if(startOfAlias<0&&!additionalAlias){startOfAlias=i+1;}continue;}elseif(char==="["&&i>0&&contents.charAt(i-1)==="["){if(i>1&&contents.charAt(i-2)==="!"){i-=2;}elseif(startOfAlias>=0){letreplacement=contents.slice(startOfAlias,end);contents=contents.slice(0,i-1)+replacement+contents.slice(end+2);startOfAlias=-1;end=-1;additionalAlias=false;}}elseif(char==="\n"){startOfAlias=-1;end=-1;additionalAlias=false;}}returncontents;}functionreplaceExternalLinks(contents){letend=-1;letaliasEnd=-1;letdepth=0;// moving backward allows us to replace within the loop,// so no additional memory.for(leti=contents.length-1;i>=0;i--){constchar=contents.charAt(i);if(end<0){if(char===")"){end=i;}}else{if(aliasEnd<0){if(char==="("){if(i>0&&contents.charAt(i-1)==="]"){aliasEnd=i-1;}else{// invalid linkend=-1;aliasEnd=-1;}// can skip the next characteri=i-1;continue;}}if(char==="]"){depth+=1;continue;}if(char==="["){if(depth==0){if(i>0&&contents.charAt(i-1)==="!"){// embed, jump to i - 1i-=1;}else{constreplacement=contents.slice(i+1,aliasEnd).replace("\\","");contents=contents.slice(0,i)+replacement+contents.slice(end+1);}end=-1;aliasEnd=-1;}else{depth-=1;}continue;}}}returncontents;}
The text was updated successfully, but these errors were encountered:
Edit to above code: added depth logic to count brackets. A simple workaround (escaping brackets) didn't work with pandoc-fignos, so I had to add logic that counts depth and only does replacement at zero depth (the actual caption and not inside some bracketed span in the caption) which is the better solution anyway.
Note to other pandoc users: Do not escape brackets inside captions
Advanced anchor texts (i.e. figure captions) can be messed up by the current version of "Remove Links" built--in, so I wrote my own.
In particular, if you have pandoc references
[@Reference]
or math with vertical bars$\left|M\right|$
you may benefit from this. It essentially removes the logic to cleanse vertical bars from link anchor text, checks for newlines and skips removal if it finds a matching pair of brackets but not on one line, and skips escaped brackets.I used ChatGPT so it also adjusted the logic which makes it hard for me to suggest a 1:1 request to update the code as it's built in.
Details
The text was updated successfully, but these errors were encountered: