Script to open MD files in Logseq if they are in a graph

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.

The most simple approach would be to change your journal title format:

to something more feasible, like:

 :journal/page-title-format "yyyy MM DD"

It looks like Logseq apply the new preference to new journal entries only, I didn’t investigate if there is a way to migrate previous pages.

Or maybe we should look for something that translate date formats.

Hello, I have created the file, moved it to ~/.local/bin, and checked the box to make it executable, but it doesn’t show up in the list of apps to run an .md file. Is there something I’m missing? Thanks!

You have to add it manually. In KDE Plasma it can be done from System Settings > File Association or from right click on a .md files > Properties. In the dialog to add an application, there is a button to select the script from its folder. In desktop environments other than Plasma it should be the same. Otherwise, create an app launcher (a .desktop file) in ./local/share/applications and set the script as the exec command. Then set that “application” as the default to open .md files.

Hiya, I’m using Ubuntu Jammy Jellyfish, just so you know. Here’s some screenshots I’ve just taken with the list of applications I get when I try to open a .md file, and the .desktop file I created. Will reboot and try again, in case that helps.




Screenshot from 2023-12-30 16-27-01

Ah, and because I left it out, I did check the boxes that enable the script and .desktop file to run as programs under properties → permissions. Sorry about that

Everything seems fine to me. In theory you should have logseq-open in Open With dialog.

Maybe try to set it as default to open markdown files manually, the command should be:

xdg-mime default logseq-open.desktop text/markdown