Skip to content

Instantly share code, notes, and snippets.

@crisrojas
Created February 21, 2023 18:32
Show Gist options
  • Save crisrojas/55e5c005f7e888ee0a380660b2dba8d5 to your computer and use it in GitHub Desktop.
Save crisrojas/55e5c005f7e888ee0a380660b2dba8d5 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
// Remove any existing "content" directory and re-create it
system("rm -r content");
system("mkdir -p content/images");
$db = new SQLite3("database.sqlite");
$result = $db->query("SELECT * FROM `ZSFNOTE` WHERE `ZTRASHED` LIKE '0'");
$row = $result->fetchArray();
$bear_img_dir = "Local Files/Note Images/";
// For each note...
while ( $row = $result->fetchArray() ) {
$title = $row['ZTITLE'];
$text = $row['ZTEXT'];
$date = $row['ZMODIFICATIONDATE'];
// @todo: if encrypted note, $text = "Private note"
// $encrypted = $row['ZENCRYPTED']
// if ( $encrypted == '' ) { skip }
# find all [image:] tags within the note
preg_match_all("/\[image\:.*?\]/mis", $text, $images);
# for all found matches
# - extract the path
# - copy the image file to content/images
# - replace the [image:] with <img> referencing the new file
# Bear stores each image in a note related subdir, but here they all end up in the same dir
foreach($images[0] as $image) {
global $bear_img_dir;
preg_match("/\[image\:(.*?)\]/mis", $image, $matches);
$image_file_path = $matches[1];
system("cp -R \"" . $bear_img_dir . $image_file_path . "\" content/images/");
preg_match("/.*\/(.*)$/", $image_file_path, $image_subpath_match);
// @todo: export other assets
// @todo: Hugo doesn't support espaces when linking images
// This could be solved by wrapping the link: ![](<images/image with spaces.png>)
// But that will break the thumbnail regex on the search component
$text = preg_replace("/\[image\:" . preg_quote($image_file_path, "/") . "\]/", "![](images/" . $image_subpath_match[1] . ")", $text);
// $text = preg_replace("/\[image\:" . preg_quote($image_file_path, "/") . "\]/", "<img src=\"images/" . $image_subpath_match[1] . "\">", $text);
}
$fp = fopen("content/$title.md", "w");
fwrite($fp, $text);
fclose($fp);
}
$db->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment