1

I want to append to a dictionary file where the rule is: entry<2TABS>alias1<1TAB>alias2. 2 TAB characters after the main entry and a single TAB character between each alias.
Because the iOS Shortcut app cannot use \t in the replacement line, I am trying to use javascript.

I am trying to make aliases for a Heading in Markdown. E.g.:

Greek#Pre-Greek Pre-Indo-European Languages => [[Greek#Pre-Greek Pre-Indo-European Languages]]<2 TABS>Greek#Pre-Greek Pre-Indo-European Languages<1 TAB>Pre-Greek Pre-Indo-European Languages

The following should be changed to Javascript code:

Match: (.*?#)(.*)

Replace: [[$1$2]]\t\t$1$2\t$2


The reason I need it is because 8 spaces for 2 Tabs and four spaces for 1 Tab don't cut it. The plugin in Obsidian cannot interpret the new lines appended to the dictionary file.

Thanks in advance,

Z.

zanodor
  • 137
  • 6

1 Answers1

2

I modified my earlier answer to favour the app Scriptable (used in conjunction with Shortcuts; run it as Inline Script), where is no need to use semi-colons on line ends and we can use return(newStr) on the last line. In the following step in Shortcuts, set the Output to a variable, which we can do whatever we want with.

Javascript code

const str = "<Provided Input>"
const regex = /(.*?#)(.*)/
const newStr = str.replace(regex, "[[$1$2]]\t\t$1$2\t$2")
return(newStr)
  • I put Provided Input to show it can be a text you manually input to try out the code. If it works (it does, for me), you can embed the code into your shortcut. If you target text selection, you'll need Shortcut Input, or a variable with that value.
  • Should you want extra aliases, combine the Shortcut Input with Provided Input (ask for input), and just add those with \tExtraAlias1\tExtraAlias2.
    Thread found here:
    https://forum.obsidian.md/t/aliases-for-headings-with-the-various-complements-plugin/58733

Another, more elaborate way with lowercase versions included:

// Input-string
let inputString = "Shortcut Input";

// Regex pattern
let pattern = /(.*?#)(.*?)(?:\||$)/;

// Replace function
let replaceFunction = (match, p1, p2) => {
  let lowercaseP1 = p1.toLowerCase();
  let lowercaseP2 = p2.toLowerCase();
  return `[[${p1}${p2}]]\t\t${p1}${p2}\t${p2}\n[[${p1}${p2}|${lowercaseP2}]]\t\t${lowercaseP1}${lowercaseP2}\t${lowercaseP2}`;
}

// Replace using regular expression and function
let exchangeString = inputString.replace(pattern, replaceFunction);

//Add Customaliases
const str = exchangeString;
const regex = /(.*)/;
const newStr = str.replace(regex, "$1\tExtraAlias1\tExtraAlias2")
return(newStr)

Output of my example

"[[Greek#Pre-Greek Pre-Indo-European Languages]] Greek#Pre-Greek Pre-Indo-European Languages Pre-Greek Pre-Indo-European Languages"

  • Mind you, in my editor, the tabs are correctly shown, but once my answer is rendered out in HTML on this site, they are substituted by single whitespace characters.
zanodor
  • 137
  • 6