nobiot / md-roam

Use Org-roam with markdown files by adding Md-roam as a plug-in. Mix org and markdown files in a single Org-roam database.
GNU General Public License v3.0
336 stars 15 forks source link

Error following with org-roam-ui error rx ** range error #81

Closed rickalex21 closed 10 months ago

rickalex21 commented 11 months ago

Hello, looking to move to markdown files to org roam. So far I manage to get one node to link but I am getting several errors. Hopefully I can get all files linked. I know the program works fine because I am able to run commands inside the markdown buffer (e.g., md-roam-get-tags etc..).

When I change to a markdown file org-roam-ui will not follow it, instead I get this error:

Error in post-command-hook (org-roam-ui--update-current-node): (error "rx ‘**’ range error")

Another error:

Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer......somefile.md

Debugger info:

Debugger entered--Lisp error: (error "rx ‘**’ range error")
  error("rx `%s' range error" **)
  rx--translate-bounded-repetition(** (1 0 "*"))
  rx--translate-**((1 0 "*"))
  rx--translate-form((** 1 0 "*"))
  rx--translate((** 1 0 "*"))
  rx--translate-seq((line-start (** 1 0 "*") " "))
  rx--translate-form((seq line-start (** 1 0 "*") " "))
  rx--translate((seq line-start (** 1 0 "*") " "))
  rx-to-string((seq line-start (** 1 0 "*") " "))
  org-element-headline-parser(nil fast)
  org-element--parse-to(153)
  org-element-at-point()
  org-back-to-heading(t)
  org-back-to-heading-or-point-min(t)
  org-roam-id-at-point()
  org-roam-ui--update-current-node()

Not sure if this is related to #80

Thanks

nobiot commented 11 months ago

I would like to fix this issue if I can but I am unable to reproduce it. Could you give me more detail (eg. file samples, step-by-step description, org version). I am afraid I won't be able to help you if you are using Doom or unreleased version of Org or Emacs.

rickalex21 commented 11 months ago

Im using vanilla Emacs. I think I know where the problem is, I've come across this issue before. This usually happens when org code tries to execute in a non-org buffer. This is why org-roam has this in the help page about org-roam-extensions:

org-roam-file-extensions is a variable defined in ‘org-roam.el’.

Its value is ("org" "md")
Original value was ("org")

List of file extensions to be included by Org-Roam.
While a file extension different from ".org" may be used, the
file still needs to be an ‘org-mode’ file, and it is the user’s
responsibility to ensure that.

In your docs you suggested that we use md? In this variable org-roam-file-extensions, if you remove md from the list, org roam now works fine with no errors. This would explain the error caused in #80 .

REPRODUCE

  1. Make sure no markdown files are open, kill all buffers except your current one, a non markdown file.
  2. open up a markdown file
  3. Start typing and saving eventually you will get an error because org funcs are being executed in a markdown file.

SOLUTION

If the problem is that org roam is executing code in the markdown file because of a variable...

This is my best guess. Figure out where or how roam is executing functions in the markdown file. Roam was not designed to execute org functions in a markdown file but we are instructing it to do that when we do : (setq org-roam-file-extensions '("org" "md"))

Another solution would be to create your own function that would update the roam database by passing the org-roam-file-extensions variable?

TAGS

On another note. I don't use obsidian anymore but I have lots of files that I would like to keep with the obsidian tags. I was wondering why did you add 1 capture group that ignores forward slash and the letter "s"? Another thing is why not make md-roam-regex-tags-zettlr-style more flexible by allowing users to use their own regex instead of adding a substring to remove your # or @?

I had to modify the code to allow obsidian tags like this, more flexible. I don't understand why the 1st capture group is needed in the code. Your code is here: line 726

;; Allow obsidian tags
(setq md-roam-regex-tags-zettlr-style "\\([[,]+[ ]?\\)\\([[:alnum:]_-]+\\)[] ]?")

;; My modified code, more flexible. Removed substring

(defun md-roam-get-tags ()
  "Get tags defined in the Zettlr style within frontmatter."
  (let ((endpoint (md-roam-get-yaml-front-matter-endpoint)))
    (cond (endpoint
           (save-excursion
             (let (tags)
               (goto-char (point-min))
               (while (re-search-forward md-roam-regex-tags-zettlr-style endpoint t)
                 (let ((tag (match-string-no-properties 2)))
                   (when tag
                     (setq tags
                           ;; Remove the first char @ or #
                           ;; (append tags (list (substring tag 1)))))))
                           ;; HACK: 12/28/2023 to allow [ tag, tag, tag] for obsidian tags.
                           (append tags (list tag))))))
nobiot commented 11 months ago

When I change to a markdown file org-roam-ui will not follow it, instead I get this error:

I don't have the error message. No error on my end. See the screen cast below I just took now. I could not include my mouse cursor in the frame, but I hope you see me opening two different markdown notes from within org-roam-ui.

Perhaps you do it different way to get the error?

https://github.com/nobiot/md-roam/assets/12507865/45ad85c5-3afc-4145-a9af-87109faacb9f

nobiot commented 11 months ago

I had to modify the code to allow obsidian tags like this, more flexible. I don't understand why the 1st capture group is needed in the code.

It's good that you have your own hack that works for you.

I tried your regexp. It does not seem to capture my tags with a "#", like this: #tag1 #tag2. These are also Obsidian tags.

To create a tag, enter a hash symbol (#) in the editor, followed by a keyword. For example, #meeting.

rickalex21 commented 11 months ago

Perhaps you do it different way to get the error?

If you can't reproduce then this is going to be a tough one... I will have to go back to square one.

I tried your regexp. It does not seem to capture my tags

It's not suppose to capture a hash, the idea is to let the user decide on how to capture tags. I don't think "#" is valid in yaml as this user mentioned here. Which would make the use of "#" in front matter less portable.

A better, more flexible, and future proof alternative would be to let the user develop their own method of obtaining tags though md-roam-regex-tags-zettlr-style don't you think so? I think I stated using the system of [tag1,tag2... because both Hugo and obsidian use it.

A change to md-roam-get-tags would not break anyone's code. When the update is pulled they will get an updated default regex. Even if it did break someone's package it wouldn't be as bad as the breaking changes that highly popular emacs packages make.

The updated defvar md-roam-regex-tags-zettlr-style regex would include "#@" . The updated function md-roam-get-tags would be changed like this:

(when tag (append tags (list tag)))

The gist is to allow a user the flexibility to use their file tags anywhere Hugo, obsidian, emacs ... If a user has a toolchain setup to publish with Hugo now they need to go back into the countless files and change all the tags from "#tag2 #tag3" etc... to something that is valid.

nobiot commented 10 months ago

Regarding the tag, see be9ae73.

I need '#' and/or '@' as prefix to the tag -- this is my usage and will not introduce a breaking change for my own use. If you don't use the prefix, this change will let you do so.

With regard to the main issue of the org-roam-ui error, I really can't reproduce it so I won't be able to look at it.

nobiot commented 10 months ago

Hopefully d05d867 fixes the issue of the main error. Let me know how you go. closing for now.

rickalex21 commented 10 months ago

Thanks for the tags update, unfortunately I am still getting this error when switching to a markdown file.

⛔ Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer arch.md> (markdown-mode)

Once I disable md-roam-mode , the error goes away.

It looks like maybe your code somewhere is calling org-element-at-point or there is a function that calls it in the markdown file? Which in org mode is valid but not elsewhere like a markdown file. The problem could be in the line 190 containing the minor mode, but at this point I'm just speculating.

nobiot commented 10 months ago

Could you make sure you are using the latest commit please? Ensure that your Emacs is not using a stale compiled script.

If the issue still persists, perhaps you can share arch.md. I struggle to reproduce the issue. I suspect the content of your markdown file gives us a clue.

rickalex21 commented 10 months ago

What do you mean by stale compiled script?

It happens with any markdown file. I do this:

  1. Open an org file and and md file.
  2. Make sure org roam UI is open and follows
  3. Switch from the org file to the markdown file to see the error.

I made a video, perhaps this will help:

https://youtu.be/u7Gh6pQ-sWg?si=IskprqQvHAZDXQ7E

nobiot commented 10 months ago

Does the error happen when you don’t use org-roam-ui?

nobiot commented 10 months ago

I cannot reproduce the issue. I can switch between org and md files with org-roam-ui shown.

Please mute your speaker; there is my loud typing noise and I could not turn off the sound recording.

https://github.com/nobiot/md-roam/assets/12507865/5564c73c-f4e2-41b0-8330-3a6ff92713e0

What do you mean by stale compiled script?

If you compile a .el script file, you get .elc. It can stay as it is after you have updated the original .el script. In this case, the .elc is older than the updated .el (stale). You think you are running the latest commit, but in fact your Emacs is still using the old code with .elc. Please make sure you delete or update the .elc file.

rickalex21 commented 10 months ago

I made you a repo to reproduce the error, rename your emacs config and use this minimal config.

git clone https://github.com/rickalex21/emacs-min.git 

I have reproduced this error both on Mac and Linux. You can see the error in this video with the emacs-min repo:

https://youtu.be/yNeny88ibIw?si=0b6UIQ_5GWHyvhqq

Not sure what your setup is but you need to view the async log, once it gets stuck somewhere on display numbers or whatever close it and open it up again. It will continue when it left off. Once you no longer see anything in messages an async log as far as compiling then the files have finished compiling and you can start using emacs.

Steps to reproduce:

  1. Let emacs do all its compiling
  2. Make sure md-roam-mode is enabled c-h v md-roam-mode
  3. open both files fake.org and fake.md like in the video. :e ~/.config/emacs/org/fake.org :vs fake.md
  4. Click on fake.org
  5. M-x org-roam-ui-open The browser is now open following you.
  6. Go up between headings Org file and Testing in the fake.org to confirm it's following.
  7. Click on fake.md the error occurs:
Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer fake.md> (markdown-mode)
nobiot commented 10 months ago

Can you remove evil and check if you can still repro the issue please? I cannot use vim key-bindings.

Also how do I view the async log?

nobiot commented 10 months ago

I tried your repo and commented out evil. I cannot reproduce the error...

https://github.com/nobiot/md-roam/assets/12507865/a0c47efc-11f2-4654-917b-8900f800e7ca

My Emacs version is GNU Emacs 29.1.50. Not sure if it is relevant.

I don't know how evil can be relevant here but try removing it and see if it makes any difference. Other than that, perhaps the Emacs version may be also relevant.

nobiot commented 10 months ago

Okay, finally I think I can reproduce the issue. It's probably something in org-roam-ui. I will have a quick look.

nobiot commented 10 months ago

I think it's this line in your configuration: https://github.com/rickalex21/emacs-min/blob/main/init/org-roam-init.el#L28

The call to org-element-at-point seems to be causing this error. On my end, commenting out seems to solve the problem.

Can you remove/comment it out and try again, please?

rickalex21 commented 10 months ago

It still does it with org-roam-node-formatter comment out. If you don't get it can you try disabling and enabling again md-roam-mode ? I noticed that sometimes I had to do this right now to get it to error again with my personal config but with the testing config it came up right away.

I am using:

This is GNU Emacs 29.1 (build 2, x86_64-pc-linux-gnu, GTK+ Version
 3.24.38, cairo version 1.16.0) of 2023-08-30, modified by Debian

I presume I can boot in to void and see if I can reproduce there cause this is a pure gtk version of emacs. Not sure if it will make a difference.

Update: Tested it on void, I'm getting the same error even with commenting. It looks like the error may have originated in org-roam-ui--update-current-node?

Debugger entered--Lisp error: (error "rx ‘**’ range error")
  error("rx `%s' range error" **)
  rx--translate-bounded-repetition(** (1 0 "*"))
  rx--translate-**((1 0 "*"))
  rx--translate-form((** 1 0 "*"))
  rx--translate((** 1 0 "*"))
  rx--translate-seq((line-start (** 1 0 "*") " "))
  rx--translate-form((seq line-start (** 1 0 "*") " "))
  rx--translate((seq line-start (** 1 0 "*") " "))
  rx-to-string((seq line-start (** 1 0 "*") " "))
  org-element-headline-parser(nil fast)
  org-element--parse-to(128)
  org-element-at-point()
  org-back-to-heading(t)
  org-back-to-heading-or-point-min(t)
  org-roam-id-at-point()
  org-roam-ui--update-current-node()

The other error:

Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer fake.md> (markdown-mode)
⛔ Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer fake.md> (markdown-mode)
nobiot commented 10 months ago

Can you try this fix, please? Just pushed it: 40c20d0

rickalex21 commented 10 months ago

Now it can follow and the node is recognized :) . However, when I try to edit the frontmatter I am getting a new error:

⛔ Error (websocket): in callback `on-open': Wrong type argument: stringp, nil
⛔ Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer fake.md> (markdown-mode)
⛔ Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer fake.md> (markdown-mode)
⛔ Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer fake.md> (markdown-mode)

If you want to reproduce try this. It's similar to the original one. Then edit the front matter and the error will occur.

---
title: Test File
date: 2022-06-16T20:19:25-05:00
id: someid
draft: false
tags: [sometag,sometag2]
---

# Test

Test file.

[[linux]]

Can nodes be added in the front matter? For example:

---
nodes: [[linux]] [[code]]
---

UPDATE

Above was my personal config but in the testing config minimal emacs I'm getting another error. It looks like something happens after the save hook org-roam-link-replace-all ? I edit the file and I saved it.

Debugger entered--Lisp error: (error "rx ‘**’ range error")
  error("rx `%s' range error" **)
  rx--translate-bounded-repetition(** (1 0 "*"))
  rx--translate-**((1 0 "*"))
  rx--translate-form((** 1 0 "*"))
  rx--translate((** 1 0 "*"))
  rx--translate-seq((line-start (** 1 0 "*") " "))
  rx--translate-form((seq line-start (** 1 0 "*") " "))
  rx--translate((seq line-start (** 1 0 "*") " "))
  rx-to-string((seq line-start (** 1 0 "*") " "))
  org-element-headline-parser(nil fast)
  org-element--parse-to(144)
  org-element-at-point()
  org-element-context()
  org-roam-link-replace-at-point()
  org-roam-link-replace-all()
  run-hooks(before-save-hook)
  basic-save-buffer(nil)
  save-buffer()
  evil-write(nil nil nil nil nil)
  funcall-interactively(evil-write nil nil nil nil nil)
  evil-ex-call-command(nil "w" nil)
  evil-ex(nil)
  funcall-interactively(evil-ex nil)
  command-execute(evil-ex)

A warning:

 Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer fake.md> (markdown-mode
nobiot commented 10 months ago

Before you call org-roam-node-find or any org-roam commands, can you ensure that md-roam is actually shown in the mode-line like in this image below?

image

I noticed that your config does not get you this mode-line. If you disable and enable md-roam again, mode-line shows it.

Then I don't get the error you report.

I think your config is incorrectly done and this is the reason for this behaviour. Instead of enabling md-roam-mode within (use-package md-roam), move it just before org-roam-db-autosync-mode like this. Using 1 or t does not matter.


(use-package md-roam
  :straight (:host github :repo "nobiot/md-roam")
  ;;:after org-roam
  ;;:disabled t
  :init
  (setq md-roam-regex-tags-zettlr-style "\\([[,]+[ ]?\\)\\([[:alnum:]_-]+\\)[] ]?")
  :custom
  (md-roam-file-extension "md"))

(use-package org-roam
;; [... omit ...]
   :config
;; [... omit ... ]
   (md-roam-mode 1)
   (org-roam-db-autosync-mode t))
nobiot commented 10 months ago

Regarding this:

---
nodes: [[linux]] [[code]]
---

I don't think markdown-mode recognize this as correct wiki-link syntax. I do not do this so I am not sure if md-roam adds these wiki links to the links database table. You can try. I am not really keen to change the current behaviour.

rickalex21 commented 10 months ago

UPDATE: I'm not 100% sure what the issue is but for now I can use company-mode by setting the variable org-roam-completion-everywhere to nil. I'm not sure what the variable enables or disables.

I'm in the talks with company-mode discussion, I'm trying to figure that out. It does look like you're right, the error does go away. I presume you don't use company-mode.

I've noticed that the linking to nodes is not the same as org mode I'm not sure how I would go about linking markdown headings? For example:

In org void.org :

* Void Linux
* * DHCP
:NODES: [[id:networking][Networking]]

I'm linking here without mentioning the word "Networking". When I see the linking in
ORUI I will see DHCP linking to Networking.

In markdown void.md :

---
title: Void Linux
---
# Void Linux
## DHCP

I have to mention [[networking]] here to link it to networking. In org
mode I would not have to do this because I can use :NODES: 

When I link I will see "Void Linux" to "networking" instead of "DHCP" to "networking".

I'm not sure what a good solution to this would be and keep the markdown syntax as markdown.

nobiot commented 10 months ago

I am not sure what where the support for :NODE: comes from (whether it's your own customization, or org-roam, or org-roam-ui).

One thing that is clear is that I will not implement md-roam support for linking to a heading within a markdown file.

Not related to the issue you are talking about, and I am sorry to say this, but I have hard time understanding the issue(s) you are reporting here.

  1. Original issue is about Error following with org-roam-ui error rx ** range error. I believe it is closed
  2. Company mode and org-roam-completion-everywhere
  3. Use of :NODE:
  4. Making a heading in markdown file as a node
  5. Linking to a heading-node in a markdown with md-roam
  6. Visualizing a heading-node in a markdown with org-roam-ui

I am happy to continue conversations and potentially I may be able to provide more help, if useful at all, to your specific issues or overall workflow you are trying to accomplish. In exchange, I request that you also respect some boundaries as md-roam is my personal project and it may not solve all of your problems. For example, I do not work with org-roam-ui very often, so my knowledge on it is limited.

Let me suggest you consider the following:

rickalex21 commented 10 months ago

One thing that is clear is that I will not implement md-roam support for linking to a heading within a markdown file.

I understand. I was thinking about mentioning that I can't find a use case for it. I'm ok linking to the file only. The :NODES: is a property in an org heading that makes the heading link to something. I meant :NODES:, my typo.

Not related to the issue you are talking about, and I am sorry to say this, but I have hard time understanding the issue(s) you are reporting here.

On your part, you are done. The issue is closed and I can not reproduce with a minimal config. However, there are others that don't use a minimal config with emacs. Other people use company mode, flyspell, yasnippet, and other popular packages. These packages may or may not conflict with your package or org-roam. What I have figured out and discovered I have been reporting on here so that someone else perhaps could find it useful if someone else comes across the same issue.

my personal project and it may not solve all of your problems.

I understand, that's why I was thinking I can't find a use case to link markdown headings. I'm ok with linking to the file as opposed to linking to the markdown heading.

Make it clear what you wish to accomplish

I want to use md-roam with no errors and all my packages. This requires me spending some time troubleshooting the errors with your package whether they are triggered by your package or not because I'm not an elisp expert that can identify the issue quickly and resolve it. Therefore, I have to try different things, troubleshoot, and asking for help when needed.

You seem to be trying to remove all the warnings and errors. I am not sure if this is going to be helpful for you to achieve your goal (eg "take good notes").

Yes I do want to get rid of the errors because I can't edit markdown files with all the errors.

Organize the issues into smaller chunks, and raise an issue for each.

I did not want to constantly be opening issues on your github. If you had Github Discussions like other GitHub Emacs packages, perhaps I would have gone off on tangents.

I think the issue will your package will continue weather it's your issue or not because there's not enough people using your package to experience the issue or willing to take the time like I did to help troubleshoot it. Part of me want's to move on and another part of me wants to open an issue with org-roam because the errors do go away when I set org-roam-completion-everywhere to nil but I'm afraid they are going going say we don't support markdown files.

I do appreciate all your help, you have been very helpful. Thanks for all the great work you have done.

nobiot commented 10 months ago

If you wish to get errors resolved, I suggest that the only way is to create an issue with minimal configuration for each case. This is the only practical way to get it fixed by getting to the root cause.

For org-roam-completion-everywhere, I can reproduce the error. See #82. I will not be able to provide a fix any time soon -- please consider this option unsupported.

rickalex21 commented 10 months ago

For org-roam-completion-everywhere, I can reproduce the error. See https://github.com/nobiot/md-roam/issues/82. I will not be able to provide a fix any time soon -- please consider this option unsupported.

I'm glad we figured it out, thanks.