Importing From Bear

I’m aware of this thread and this one but did anyone write an action to import a note from Bear, complete with copying the tags over?

(I don’t mind whether it’s driven from Bear or driven from Drafts - though I suspect the former is easier.)

I had already been using https://github.com/TehShrike/backup-bear-notes to backup my Bear notes into MD in Dropbox so my process is:

  1. Backup Bear into MD as above
  2. Split Bear nested tag into discrete tags with an action (can’t remember where I got this from, but I think I amended something and apologies if I have not attributed):
const re = /#[\/\w\d]+/g;
const masterTag = draft.content.match(re);

// define regex to use...
const findRegex = /\//g;
// define replacement expression...
const replaceWith = "\n#";

const temp = masterTag[0].replace(findRegex, replaceWith);

// do the replacement...
draft.content = draft.content.replace(masterTag, temp);
draft.update();
//alert (masterTag);
  1. use the hashtag to tags action to convert to Drafts tags

It works well for me. The only thing missing from Drafts is nested tags! :smile: -)

3 Likes

Wow - this is so cool, @Nick_Wild! Quite honestly, I don’t really understand what that all means, but it’s neat to just see this, and know that one day I may understand it :slight_smile:

1 Like

:smile: I liked doing in multiple steps, but for ease I have also aggregated into one script that you can copy into a script action.

You do need to export Markdown from Bear still though.

//split Bear nested hashtag into individual lines with #
const reg = /#[\/\w\d]+/g;
const masterTag = draft.content.match(reg);

// define regex to use...
const findRegex = /\//g;
// define replacement expression...
const replaceWith = "\n#";

const temp = masterTag[0].replace(findRegex, replaceWith);

// do the replacement...
draft.content = draft.content.replace(masterTag, temp);

//convert new hashtags to Drafts tags
// This script builds on kjaymiller script to find hashtags inside drafts. Original script: http://actions.getdrafts.com/a/1ME

// find tags in content
var re = /#[\w\d]+/g;

// Tag drafts
	var tags = draft.content.match(re);
	for (tag in tags) {
		draft.addTag(tags[tag].substring(1));
		}
		
//remove temp hashtags
	draft.content = draft.content.replace(re,"");
	draft.content = draft.content.trim();
		
// restore nested hashtag		
draft.content = masterTag + "\n" +  "\n" + draft.content ;	
draft.update();

1 Like

So, I think this assumes tags are only preceded by the octothorpe. Mine can be multi-word so mine are all terminated with one, too.

I can adjust, tho’.

Yes that’s right @martinpacker personally I only use singleword tags in bear.

You could just change the regular expression to account for Bear-style delimited tags. I had a quick go at writing one just now (barely tested, haven’t checked it’s suitable for the specific script mentioned above).

Expression: (?<!\w)\#[^\#\n\r]+?\#(?!\w)|\#[A-Za-z\d-]+

Text: This is a #hashtag and a #12tag3! Also a #special kinda tag# here too.#<- not a tag.

Matches: “#hashtag”, “#12tag3”, “#special kinda tag#”

Probably needs tweaking for edge cases and possibly performance, but you get the idea.

2 Likes