As requested by someone here there is a Bash script for Linux (I can’t port it to MacOS but it should be trivial, one just need to replace urlencode and xdg-open).
If this script is assigned as default program to open Markdown files it can be used to open the corresponding page in Logseq. If the file is not a page in a graph, it’s open in the editor you specify.
Sorry but to keep it simple it assumes Logseq is running and the target graph is listed in Logseq (if it’s not the current graph another window will be open). Also, it doesn’t handle emojis in file names.
#!/bin/bash
# Use the following to specify another editor
# as a fallback to open Markdown files that are
# not in a graph:
fallback="kate"
# Check if the input file is in a graph folder
# by checking if a Logseq config file is present in
# the same folder or in the parent one and so on.
filepath=$(dirname "$1")
while [ ! -e "$filepath/logseq/config.edn" ]; do
if [ "$filepath" = "/" ]; then
$fallback "$1"
exit 0
fi
filepath=$(dirname "$filepath")
done
# Generate the URI by looking at the graph folder
# name and at the file name.
# The 's/___/%2F/g' part handles pages in namespaces.
graphname=$(basename "$filepath")
filename=$(basename "$1" | sed 's/\.md$//')
encoded=$(urlencode "$filename" | sed 's/___/%2F/g')
xdg-open "logseq://graph/$graphname?page=$encoded"
In case you are new to Linux, you need to save the code above in a text file with the name you prefer, for example logseq-open, mark it as executable and move it to a folder in your $PATH. Finally, edit your system settings to assign this script as the default program for Markdown files (these are all things you can easily find with Google or whatever).
I just ported it to MacOS. Important is, that you can apparently not use a .sh script as your default application for all .md files in MacOS. A workaround is to create an .app application through Apple Automator. Ironically, the best tutorial for this a quick search turned up, is for the exact same purpose – just for use with Obsidian: Make Obsidian a default app for Markdown files on macOS - Share & showcase - Obsidian Forum
You don’t have to make a .sh file, mark as executable, add to $PATH … if you do it that way. You can omit the Java-Script bit in the above tutorial. You only need the “Run Shell Script” Action and the following code (slightly adjusted for MacOS):
#!/bin/bash
# Use the following to specify another editor
# as a fallback to open Markdown files that are
# not in a graph:
fallback="code"
# Check if the input file is in a graph folder
# by checking if a Logseq config file is present in
# the same folder or in the parent one and so on.
filepath=$(dirname "$1")
while [ ! -e "$filepath/logseq/config.edn" ]; do
if [ "$filepath" = "/" ]; then
$fallback "$1"
exit 0
fi
filepath=$(dirname "$filepath")
done
# Generate the URI by looking at the graph folder
# name and at the file name.
# The 's/___/%2F/g' part handles pages in namespaces.
graphname=$(basename "$filepath")
filename=$(basename "$1" | sed 's/\.md$//')
encoded=$(ruby -e 'require "erb"; puts ERB::Util.url_encode(ARGV[0])' "$filename" | sed 's/___/%2F/g')
# Use the open command to open the URI
open "logseq://graph/$graphname?page=$encoded"
That code assumes, that you have Visual Studio Code installed, and on your path. I did not get it to open as fallback edit as of now though.
There seems to be another issue: All pages that are not journal pages are fine, but jornal pages don’t open.
logseq://graph/mygraph?page=Feb%2017th%2C%202023 is for example a Page URL for todays page.
The .md file however is called 2023_02_17.md.
Catching that will need some more mangling of the input. Think you could do that @alex0 ? Love it already, even if is does not work for daily notes!
It’s even better, if one installs QL Markdown – that allows macs quick look feature on markdown files directly from the Spotlight results. Just arrow down to the .md file where there was a hit and pressing Spacebar will open a nice preview of the .md file.