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: -)

4 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

Hi Martin. Did you ever succeed with this? I have notes with material like this at the bottom that I’m trying to convert to Drafts tags:

#highlight/self-fluency/Cross Training Your Attention# #highlight/instapaper

Not sure how to regex this type of text into 4 different tags:

  • highlight
  • self-fluency
  • Cross Training Your Attention
  • instapaper

That’s a… weird format.

Assuming it’s on the last non-blank line of a draft, this should add all the mentioned tags to the draft. You can uncomment the alert line near the bottom to see a list of them in alphabetical order.

var lines = draft.content.trim().split('\n');
var lastLine = lines[lines.length - 1];
var tagsRE = /\#([^\#\n\r]+?)\#(?!\w)|\#([A-Za-z\d-\/]+)/gi;
var matches = lastLine.matchAll(tagsRE);
var tags = [], tagStr, tag;
for (var match of matches) {
	tagStr = (match[1] != undefined) ? match[1] : match[2];
	for (tag of tagStr.split("/")) {
		if (!tags.includes(tag)) {
			tags.push(tag);
		}
	}
}

//alert("Tags are:\n\n" + tags.sort().join(", "));
tags.forEach(t => draft.addTag(t));
1 Like

Thanks for this! Wow, I’ll be trying to study this to figure it out. Works great!

And the format is Bear’s way of creating “folders”. The slashes between the tags create a hierarchy you can navigate in the sidebar. It’s weird but useful. I sort of wish the tags weren’t just hashtags in the text, though, and were instead metadata like in Drafts. Oh well, I’m departing the app in the end anyway.

1 Like

You’re going to be disappointed at my answer: I didn’t get on that well with Bear and had only a few notes (blog posts) so I manually transferred them and haven’t used Bear since.

Future blog posts will be written in Drafts and saved to DropBox. (Would be iCloud if I could get that worked out.)