Previewing Without Double-square-brackets

Kevin Kleinfelter's Avatar

Kevin Kleinfelter

22 Mar, 2017 03:51 PM

I often use Marked2 to preview my nvalt notes. Nvalt notes are often littered with [[HyperLinkThis]]. That renders as ugly plain text in Marked2.

I'd like to set up Marked2 with a preprocessor, so that "[[HyperLinkThis]]" gets replaced with "[HyperLinkThis](nv://HyperLinkThis)".

i.e. I want the text sent from nvalt (or any other editing program) to be run through a filter to transform non-markdown markup into proper markdown markup.

I think this might be possible via the Advanced preferences tab and a custom preprocessor. Has someone done this before, so I don't reinvent the wheel? Can someone point me in the right direction?

  1. Support Staff 1 Posted by Brett on 22 Mar, 2017 09:28 PM

    Brett's Avatar

    This is possible with a basic preprocessor that does a regular
    expression substitution. Something like this in Ruby:

     #!/usr/bin/ruby
     # encoding: utf-8
     require 'cgi'
    
     input = STDIN.read.force_encoding('utf-8')
    
     puts input.gsub(/\[\[(.*?)\]\]/) {|m|
       match = Regexp.last_match
       "[#{match[1]}](nvalt://#{CGI.escape(match[1])})"
     }
    

    I can help you set that up if needed.

    -Brett

  2. 2 Posted by Kevin Kleinfelt... on 23 Mar, 2017 10:48 PM

    Kevin Kleinfelter's Avatar

    This is great. Thanks!

  3. 3 Posted by Michael on 21 May, 2021 04:27 PM

    Michael's Avatar

    Hey Brett — Is the above still the best way to handle pre-processing wikilink formatting? I'm having trouble getting it to work. Should I have the code above in a .rb file? I've got that dropped into my documents folder. Do I need to include an argument? Permissions are enabled. Not sure what I'm missing. Any help is much appreciated.

  4. Support Staff 4 Posted by Brett on 21 May, 2021 04:43 PM

    Brett's Avatar

    @theredhype Yes, it needs to go in a .rb file, and you need to make it executable by running chmod a+x path/to/file on the script. In Preferences->Advanced you need to make sure you have the full path to the script. Assuming all of that is in place, check the Custom Processor logs under the Help menu and see if you're getting any errors.

  5. 5 Posted by Michael on 21 May, 2021 05:03 PM

    Michael's Avatar

    Thanks for the quick response.

    I've now made the file executable, and confirmed by running ls -l which returns -rwxr-xr-x as expected.

    In the advanced preference of Marked 2, I'm using the path selector to select the .rb file as my pre-processor (assuming that satisfies ensuring the path is perfect and full). I get a little green OK next to the file. I'm leaving the args field blank. updating permissions. refreshing. But no change.

    I've turned Debug mode on for all, and opened the log file. However, while fiddling with all the settings I'm unable to get the log to show anything at all in either processor or pre, stdout or stderr. All remains blank.

    Anything else I should check?

  6. Support Staff 6 Posted by Brett on 21 May, 2021 05:26 PM

    Brett's Avatar

    Do you have the pre-processor turned on in the preview window? At the
    bottom right side of the window there should be a yellow "light" if the
    preprocessor is enabled. (If "Automatically enable for new windows" is
    checked, this should be on by default when you open the preview).

    If it's definitely running and you're still getting no log messages, try
    adding an `echo "TESTING"` line to the script and see if it shows up in
    the log. At least that would indicate it's running. The log will only
    update after a refresh, so be sure to hit ⌘R when testing.

    -Brett

  7. 7 Posted by Michael on 21 May, 2021 05:46 PM

    Michael's Avatar

    You caught me! I did not have it enabled. It is now enabled. Checkmarked. And yellow lit. I had checked "Automatically enable for new windows, but it's possible I had not closed and re-opened a new window. I did not realize I needed to turn it on.

    Still not quite there though. I'm getting the following error:

    ERROR: Custom processor is not executable
    
    Please ensure that your user has permission to execute markedwikilinks.rb
    
    Due to sandboxing restrictions, some binaries are not accessible from the Mac App Store version of Marked 2. To crossgrade to the (non-sandboxed) Paddle version of Marked for free, use Help->Crossgrade.
    

    Also, Marked 2 has started crashing now, as it tries to preprocess I suppose. I've saved the crash report, in case that becomes helpful.

    I checked my file permissions again, and they appear correct... I think. (ref. screenshot)
    `-rwxr-xr-x@ 1 mrno staff 219 May 21 09:14 markedwikilinks.rb

    I've got this file in my documents folder, and have refreshed permissions, and also accepted the system dialogue about accessing files which pops up. A peek at the system prefs seems to confirm access. (ref. screenshot).

    Do I need to Crossgrade?

  8. Support Staff 8 Posted by Brett on 21 May, 2021 06:21 PM

    Brett's Avatar

    Crossgrading would probably be the easiest solution. Another option to try is changing the processor to call ruby directly:

    Path: /usr/bin/ruby
    Args: /Users/mrno/Documents/markedwikilinks.rb
    

    That might circumvent the executable permissions issue. No guarantees, sandboxing is complicated with shell scripts.

    You can also try adding Marked to Full Disk Access in Security & Privacy if it isn't already.

  9. 9 Posted by Michael on 21 May, 2021 06:27 PM

    Michael's Avatar

    Thanks for confirming.
    Crossgrading has solved the issue.

    Thanks for the alternative notes. If there's no other practical difference in functionality, the crossgraindg solution works great for me. I'm assuming the versions released through each channel are essentially the same.

    Thanks for all your help!

  10. 10 Posted by Michael on 21 May, 2021 06:40 PM

    Michael's Avatar

    Should I be able to stay in a Marked 2 window, and navigate between file previews, perhaps by changing the nvalt:// portion of your ruby code at the top of this file to something like Marked+2:// ?

  11. Support Staff 11 Posted by Brett on 21 May, 2021 09:41 PM

    Brett's Avatar

    Yep, the only difference between versions is the sandboxing, which means
    the direct version is actually slightly more capable than the MAS
    version.

    As far as previewing links within Marked, if you change the links to
    just be file:// urls to markdown files, you can open them in the current
    window or a new window, you should get an option. Marked doesn't have a
    back button, though, so opening in the same window is a one-way street.
    You can also make them `x-marked://open/?file=...` links to force them
    to open new windows.

    -Brett

  12. 12 Posted by Michael on 21 May, 2021 11:05 PM

    Michael's Avatar

    I may be making this harder than it needs to be. But I'm trying to keep my markdown simple with double brackets around file names. And then use the preprocessing script to render the docs linkable, so I can click around between them. Kind of like a choose your own adventure

    I've got that working nicely by using a hard path to the folder I'm in, like this:

    #!/usr/bin/ruby
     # encoding: utf-8
     require 'cgi'
    
     input = STDIN.read.force_encoding('utf-8')
    
     puts input.gsub(/\[\[(.*?)\]\]/) {|m|
       match = Regexp.last_match
       "[#{match[1]}](file:///Users/usernam/Dropbox/nvUltra/project_name/#{CGI.escape(match[1])})"
     }
    

    But I use several folders for different projects. So I'd like to make that script render a relative path, but I haven't been able to find a combination of colons, dots, and slashes that works.

    Is it possible?

    Note: I'm only using titles with no spaces in them, to keep paths simpler. And the one-way street (no back button) doesn't bother me, as the files are all interlinked anyway.

  13. Support Staff 13 Posted by Brett on 22 May, 2021 04:31 PM

    Brett's Avatar

    Ok, let me revise my suggestion. You don't need the file:// part for a Markdown link, it will work to just point it to the filename and it will assume it's in the same directory as the current file. I did realize that for this to work you need to use URI.encode rather than CGI.escape (URI will use percent encoding, CGI adds plus signs for spaces which won't work as links). I also added a little regex to add .md if it's not already there, which you might need to adjust if your default file extension isn't .md.

    #!/usr/bin/ruby
    # encoding: utf-8
    require 'uri'
    
    input = STDIN.read.force_encoding('utf-8')
    
    puts input.gsub(/\[\[(.*?)\]\]/) do |m|
      match = Regexp.last_match
      "[#{match[1]}](#{URI.encode(match[1]).sub(/(\.md)?$/,'.md')})"
    end
    
  14. 14 Posted by Michael on 23 May, 2021 01:35 AM

    Michael's Avatar

    Happy to report file names with spaces are working well! Thanks for noticing that.

    Wikilinks are still visually pretty in the Marked 2 preview, but they're not linking to the files. When I hover over the links in Marked 2, I see the notification "Missing File: Hypothesis.md" pop up at the bottom, and clicking on a link does nothing.

    If I add the file:///Users/user/Documents/folder/ path back in everything works perfectly. But of course, that will only work for the hard-coded folder of documents, rather than being relative to whichever folder I'm working in.

    Is there something else that needs to be there in place of the file:// ? I can't seem to get Marked 2 to see the files in the folder without including the full file path.

    Note: In my .rb file, I did swap out the do and end from the above code block for brackets, otherwise Marked 2 was simply displaying <Enumerator:0x00007fa233094ea8>

  15. Support Staff 15 Posted by Brett on 23 May, 2021 07:36 PM

    Brett's Avatar

    What OS version are you on? The version of the script above was tested
    and working fine on Big Sur with the bundled Ruby. Bracket syntax and
    do/end are just two variations of the same block syntax, there shouldn't
    be any operational difference between the two.

    But anyway, since that's working now, right click on a link showing
    Missing File and choose Inspect. Tell me what the actual URL it shows in
    the link markup is. As long as it doesn't have any prefix and is just
    the filename, it should work. The only thing I can think to double check
    is that the actual file has the same `.md` extension the link is looking
    for...

    -Brett

  16. 16 Posted by Michael on 24 May, 2021 06:52 AM

    Michael's Avatar

    I'm on macOS Catalina 10.15.7 (19H1030)

    ref. attached screenshot — inspect appears to reveal the URL is simply the filename?

    Maybe you'll notice something in the screenshot that I'm not.

  17. Support Staff 17 Posted by Brett on 24 May, 2021 11:31 AM

    Brett's Avatar

    Yep, that's exactly what it should be. Now assure me that in the same
    nvUltra folder there actually is a file named "Founder.md", including
    the same capitalization.

    -Brett

  18. 18 Posted by Michael on 24 May, 2021 11:39 PM

    Michael's Avatar

    Yes, it's in the same folder and named precisely the same.

    Brett! I think I've found a big clue to the mystery.

    I've been pointing Marked 2 at a parent folder — it's watching a folder called nvUltra, inside of which I have a dozen project based folders.

    When I tell Marked 2 to watch the lower level project folders, the relative paths created by the preprocessing .rb file all work perfectly as expected. I'm able to click back and forth between them without problems. No missing files, etc. I have tested this with several of my project folders and the interlinking wiki links all work great!

    Then, when I switch Marked 2 back to watching the parent, those files go missing.

    Ideally, I'd be able to leave Marked 2 pointed at the parent, so that as I move between projects (which I'm experiencing as tabs in nvUltra), it just automatically switches with me.

    But I'm guessing, if I've found the issue, this is something I can't fix on my end.

  19. Support Staff 19 Posted by Brett on 25 May, 2021 10:16 AM

    Brett's Avatar

    Yep, in a folder preview Marked treats all paths as if they were in the
    base folder, so it's not looking in subfolders for relative paths. I
    should probably change that, but I actually forgot that Marked could
    even read subfolders in folder view mode.

    I think that the MARKED_ORIGIN environment variable should be correct,
    though, so it would be simple to adjust the preprocessor to include an
    absolute (or more correct relative) path. Will have to test, though.

    -Brett

  20. Support Staff 20 Posted by Brett on 25 May, 2021 11:23 AM

    Brett's Avatar

    Yep, environment variable works. The following gets the full path to the folder of the file containing the link and applies it to the substitution. If you export a file using this, the paths won't be relative and will likely break when viewed anywhere else, but it will work for previewing.

    #!/usr/bin/ruby
    # encoding: utf-8
    require 'uri'
    
    input = STDIN.read.force_encoding('utf-8')
    source = File.dirname(ENV['MARKED_PATH'])
    puts input.gsub(/\[\[(.*?)\]\]/) { |m|
      match = Regexp.last_match
      "[#{match[1]}](#{File.join(source, URI.encode(match[1]).sub(/(\.md)?$/,'.md'))})"
    }
    
  21. 21 Posted by Michael on 25 May, 2021 07:45 PM

    Michael's Avatar

    Brilliant. Thank you Brett! The relative file paths are now working for me. I can use the same preprocessor for any path / folder.

    I think I've found one use case that is still breaking. Steps to reproduce:

    1. Open Marked 2, point it at top level folder which contains folders of .md docs.
    2. Open two or more lower level folders as tabs in nvUltra.
    3. In Marked 2, click on a wiki link and successfully navigate to another doc preview
    4. In nvUltra, edit any doc at any level in the directory being watched
    5. Result: ref screenshot: No Readable File Found

    Notes

    • This error only appears AFTER I have clicked on a wiki link in the Marked 2 preview. Never before that. If I don't click on a wiki link in Marked 2, I can continue editing any doc in any sub-directory and Marked 2 updates perfectly.
    • This error also appears even when I've pointed Marked 2 directly at the lowest level documents folders (and again, only after I've clicked a wiki link in Marked 2).
  22. Support Staff 22 Posted by Brett on 25 May, 2021 09:30 PM

    Brett's Avatar

    I think that's a bug on my end. When a preview that's watching a folder follows a link to a single file, it's now no longer a folder watcher (but it thinks it is). So when it detects a directory change and tries to load the most recent file, the folder it looks in is actually now a single file. Marked really wasn't designed to be a wiki navigator like this, so it might be a case where you're better off opening wiki links in new preview windows and leaving the folder preview as is.

  23. 23 Posted by Michael on 25 May, 2021 10:38 PM

    Michael's Avatar

    Ah, yes. Fascinating. I can add a link which lets me return in Marked to watching the top level documents directory, but I'd have to add it to every doc, and always remember to click it in Marked after clicking wiki links, and before working in the text editor again. Ha!

    I tried this with [Parent](file:///Users/User/Dropbox/nvUltra) which leads back to a top level folder I'm watching, and it appears to tell Marked to watch at that level again. I didn't get this to work with a relative path, but that wouldn't really matter for me, since all my work is ultimately in one parent folder; hard coding that path here is fine.

    Not sure whether I'd add this to my workflow. But it's an interesting workaround.

  24. Support Staff 24 Posted by Brett on 25 May, 2021 11:28 PM

    Brett's Avatar

    Just a thought, but it would be entirely possible to have the custom processor embed that link in the output, perhaps just appending it at the end. Wouldn’t be able to dynamically determine what the top level you wanted was, but even if it were hardcoded, at least you wouldn’t have to edit into all your notes.

    - Brett

  25. 25 Posted by daniel on 26 Mar, 2022 10:51 PM

    daniel's Avatar

    Is there an easy way to modify the script posted in the first reply by @Brett to make it work generically, beyond just Nvalt? Specifically I’m interested in making it work with the [[wiki links]] I use in the applocations DEVONthink and Notebooks.

  26. 26 Posted by daniel on 26 Mar, 2022 11:07 PM

    daniel's Avatar

    For clarity I should add that I want the Wiki Link to open in Marked 2 (not the other app).

  27. Support Staff 27 Posted by Brett on 27 Mar, 2022 11:25 AM

    Brett's Avatar

    @daniel, do you mean while previewing in Marked, or from within those other applications? To do it outside of Marked you would need to create a System Service or similar (maybe Keyboard Maestro) version of the script. The same concept applies, but the implementation would be different.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac