Combine two scripts--save to file with tags

I’m trying to combine two scripts: @agiletortoise Publish to iCloud and @montchr’s Tags to Hashtags. What I’m looking to do is write out every note tagged “note” to a file, but also have each of those notes’ tags written to the end of the file as hashtags. (I’m trying to import a bunch of notes into Bear with the tags converted from Drafts tags to hashtags–didn’t see an action that did this for Bear already).

Here’s what I tried:

// START configuration options

// set to sub folder path for a folder in the Drafts iCloud Drive folder.
let destinationFolder = "/Tags/";
// The template to use naming the export files
let fileNameTemplate = "[[safe_title]].md";
// Temnplate to use for the content of the files
let contentTemplate = "[[draft]]";
// Array of tag names to always include in the prompt. Recently used tags will also be included.
let alwaysIncludeTags = ["personal", "hold"];

// END configuration options

let f = () => {
	// merge tags to prompt for...
	let tags = [...alwaysIncludeTags, ...Draft.recentTags()];
	if (tags.length == 0) {
		return false;
	}	

	// prompt to select a tag to export
	let p = Prompt.create();
	p.title = "Select Tag to Publish";
	p.message = "All inbox drafts with the tag selected will be exported to files in the " + destinationFolder + " folder in iCloud Drive.";

	for (let tag of tags) {
		p.addButton(tag);
	}
	if (!p.show()) {
		return false;
	}
	let tag = p.buttonPressed;

	// query for inbox drafts with the designated tag
	let drafts = Draft.query("", "all", [tag]);
	if (drafts.length == 0) {
		console.log("No drafts matching tag found");
		return true;
	}
for (let d of drafts){
	const { content, tags } = draft;

if (tags && tags.length !== 0) {
  const spacelessTags = tags.map(tag => tag.replace(' ', ''));
  let newTags = spacelessTags;

  const re = /#[\w\d]+/g;
  const hashtags = content.match(re);

  // Get the draft tags that don't already have hashtag equivalents in the content
  if (hashtags) {
    const hashlessHashtags = hashtags.map(tag => tag.replace('#', ''));
    newTags = spacelessTags.filter(tag => !hashlessHashtags.includes(tag));
  }

  // Append the new tags to the content
  if (newTags.length > 0) {
    const newHashtags = newTags.map(tag => `#${tag}`);
    draft.content += `\n\n${newHashtags.join(' ')}`;
  }
}
}

	// write each draft to iCloud Drive.
	let fm = FileManager.createCloud();
	for (let d of drafts) {
		let fileName = d.processTemplate(fileNameTemplate);
		let path = destinationFolder + tag + "/" + fileName;
		let content = d.processTemplate(contentTemplate);
		if (fm.write(path, content)) {
			console.log("Created file: " + path);
		}
		else {
			console.log("Error creating file: " + path);
			return false;
		}
	}

	alert("Published " + drafts.length + " files to " + destinationFolder) + " in the Drafts iCloud Drive folder";
	return true;	
}

if (!f()) {
	context.fail();
}

This action created the files in iCloud Drive, but didn’t append the hashtags to them. I’m guessing the scripts aren’t linking up somehow, or the lines at the end are overwriting the content changes the hashtag script in the middle is doing. But I’m out of my javascript depth at this point :slight_smile:

Any suggestions? Thanks!