Action

Bear note with metadata

Posted by Bert, Last update almost 6 years ago

Adds the contents of the draft to a new Note in Bear, with the first line as the title.

Steps

  • script

    function uuidv4() {
    	return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
    		var r = (Math.random() * 16) | 0,
    			v = c == "x" ? r : (r & 0x3) | 0x8;
    		return v.toString(16);
    	});
    }
    
    RegExp.escape = function(s) {
    	return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
    };
    
    function to_bear(text) {
    	const replacements = [
    		{ regex: /^(\s*)- /gm, template: "$1{} ", final_str: "*" },
    		{ regex: /^(\s*)\* /gm, template: "$1{} ", final_str: "-" },
    		{ regex: /^(\s*)- \[ \] /gm, template: "$1{} ", final_str: "-" },
    		{ regex: /^(\s*)\* \[ \] /gm, template: "$1{} ", final_str: "-" },
    		{ regex: /^(\s*)- \[\s*x\s*\] /gm, template: "$1{} ", final_str: "+" },
    		{ regex: /^(\s*)\* \[\s*x\s*\] /gm, template: "$1{} ", final_str: "+" },
    	];
    
    	for (let replacement of replacements) {
    		const uuid = uuidv4();
    		replacement.template = replacement.template.replace("{}", uuid);
    		replacement.uuid = uuid.toString();
    	}
    
    	if (!text) {
    		return "";
    	}
    
    	var text = text.replace(
    		new RegExp(`^(\\s|[^az-zA-Z\\d])*${RegExp.escape(draft.title)}\n*`),
    		"---\n",
    	);
    
    	for (const replacement of replacements) {
    		text = text.replace(replacement.regex, replacement.template);
    	}
    
    	for (const replacement of replacements) {
    		text = text.replace(new RegExp(replacement.uuid, "g"), replacement.final_str);
    	}
    	return text;
    }
    
    
    to_bear(draft.content);
    
    draft.setTemplateTag("bear_content", to_bear(draft.content));
    
    draft.setTemplateTag("bear_title", draft.title.replace(/^(\\s|[^az-zA-Z\\d])*/, ""));
    
  • url

    template
    bear://x-callback-url/create?title=[[bear_title]]&text=[Draft]([[draft_open_url]])%0ACreated:%20[[created|%Y-%m-%d %H:%M]]%0AModified:%20[[modified|%Y-%m-%d %H:%M]]%0ALocations:%20[Created](https://maps.apple.com?q%3D[[created_latitude]],[[created_longitude]]%26sll%3D[[created_latitude]],[[created_longitude]]%26t%3Dm)%20[Last%20modified](https://maps.apple.com?q%3D[[modified_latitude]],[[modified_longitude]]%26sll%3D[[modified_latitude]],[[modified_longitude]]%26t%3Dm)%0A[[bear_content]]&tags=[[tags]]
    useSafari
    false
    encodeTags
    true
  • callbackUrl

    template
    bear://x-callback-url/create?title=[[bear_title]]&text=[Draft]([[draft_open_url]])%0ACreated:%20[[created|%Y-%m-%d %H:%M]]%0AModified:%20[[modified|%Y-%m-%d %H:%M]]%0ALocations:%20[Created](https://maps.apple.com?q%3D[[created_latitude]],[[created_longitude]]%26sll%3D[[created_latitude]],[[created_longitude]]%26t%3Dm)%20[Last%20modified](https://maps.apple.com?q%3D[[modified_latitude]],[[modified_longitude]]%26sll%3D[[modified_latitude]],[[modified_longitude]]%26t%3Dm)%0A[[bear_content]]&tags=[[tags]]
    waitForResponse
    false
    encodeTags
    true

Options

  • After Success Ask
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.