Appending tags to markdown files

I have markdown files I’m working with in Obsidian, but Obsidian doesn’t recognize tags (which is super irritating but I digress.)

Is it possible for a script to append the tag names in wikilinks format to the end of a markdown document? It could work one of two ways, I guess – either:
A) a person selects tag #xyz and every markdown file with that tag has [[xyz]] automatically added to the end of the text in the document, or
B) a person selects markdown file(s) 1 which is tagged with #a #b and #c. the script appends [[a]] [[b]] and [[c]] to the end of the text in the document.

i would personally prefer 1, but either would work. Is this possible?

1 Like

Yes, I guess. Both variants you describe should be possible to implement.

Sorry, I was a bit in a hurry when I wrote my first response. Below a (Java)script I suggest for the purpose. It implements approach A in our original post. As it is, it will only modify MD records in the global inbox. Please read through the comments and act accordingly to

  • modify MD records in one database, or
  • modify all MD records in all databases

Please be aware that these modifications cannot be undone - better test it first on some duplicates in the global inbox before, make a backup of your data and BE CAREFUL!
The script comes without any warranty whatsoever.

(() => {
const app = Application("DEVONthink 3");
app.includeStandardAdditions = true;
/* Remove comment and set DBname to a sensible value to work 
 * only with MD records in one database */
// const DBname: "MyDB"; 

/* Some sanity checks ...*/
const selRecords = app.selectedRecords();
const tags = selRecords.filter(r => 
  r.tagType() === "ordinary tag").map(r => r.name());

/* Check that tag(s) is/are selected */
if (tags.length === 0) {
  app.displayAlert("Please select at least one tag!");
  return;
}

/* Make sure that user wants to set more than 1 tag */
if (tags.length > 1) {
   app.displayAlert("More than one tag selected.");
   return;
}

const theTag = tags[0];
/* create Wikilink */
const wikilink = `[[${theTag}]]`;

const searchString = `tags: ${theTag} type: markdown`;
/* The following line searches for MD records only in the global inbox - useful for testing      
   Add two slashes at front to comment out. */
const MDrecords = app.search(searchString , {in: app.inbox().root()});

/* Remove the leading slashes from the next two lines and set dbName at the start to 
 * something sensible to search for MD records in _one_ database */
//const db = app.databases.whose({name: dbName});
//const MDrecords = app.search(searchString, {in: db[0].root()});

/* Remove the slashes from the next line to search for MD records in ALL databases */
//const MDrecords = app.search(searchString);

/* Nothing found, terminate */
if (MDrecords.length === 0) {
  app.displayAlert("No matching MD records found.");
  return;
}

/* Ask for confirmation to modify found records */
try {
 app.displayDialog(`Modify ${MDrecords.length} markdown files?`,
        { buttons: ["No", "Yes"],
        defaultButton: "No",
        cancelButton: "No" })
	} catch(e) {
	  return;
}

/* Update all found MD records */
MDrecords.forEach(r => {
  const txt = r.plainText();
  r.plainText = `${txt}
${wikilink}
`; 
})
})()
2 Likes

wow!!! thanks!!!

1 Like