Use Marked to open nvALT internal links

fildrake44's Avatar

fildrake44

20 Mar, 2018 04:53 PM

I am using Marked to preview nvALT markdown notes, and I am using nvALT's internal linking syntax like this:

  • [[my-file-foo]]
  • [[my-file-bar]]

These appear clickable in nvALT and take you to that note in nvALT. But in Marked, they render as plain text and not links. How can I make these links clickable in Marked 2? Ideally, clicking them should open the markdown file in either Marked 2 or even in nvALT.

For some context, I am managing a library of markdown files that link to each other, and we are trying to get Marked/GitHub/nvALT to play together nicely, with respect to showing either clickable regular markdown links or clickable internal nvALT links. Users often use nvALT to view and edit the markdown files. Since nvALT does not make regular markdown links clickable, we are trying to use nvALT internal links and make them clickable in Marked 2 / Github... which is also annoying. Unless there a scheme that makes regular markdown links clickable in nvALT (in the raw non-preview mode)?

Thanks!

  1. Support Staff 1 Posted by Brett on 20 Mar, 2018 08:31 PM

    Brett's Avatar

    nvALT's editor parses wiki links out, and the preview does it with JavaScript (unless you're using a custom template). The JS that makes it happen looks like this:

    <script>
    var container = document.getElementById("wrapper");
                container.innerHTML = container.innerHTML.replace(/\[\[(.*?)\]\]/,"<a href='nvalt://find/$1'>$1</a>");
    </script>
    

    A custom preprocessor that simply injected that into every preview would essentially add nvALT wiki support to Marked. However, it probably wouldn't help on GitHub. You'd need to write your own script that made the same regex conversion in the second line of the above script to the actual Markdown before pushing it to GitHub (though that could be added to a post-commit hook...).

    -Brett

  2. 2 Posted by David on 28 Mar, 2018 04:12 PM

    David's Avatar

    Any suggestion of how could this be implemented in Pandoc? I use it as a custom processor in Marked 2, and having functional wiki links would make my note-taking system just perfect.

  3. Support Staff 3 Posted by Brett on 28 Mar, 2018 07:20 PM

    Brett's Avatar

    David,

    Add a preprocessor that does a simple search and replace. In Ruby for example:

     #!/usr/bin/ruby
     $stdout.print STDIN.read.gsub(/\[\[(.*?)\]\]/,'[\1](nvalt://find/\1)');
    
  4. 4 Posted by David on 14 Jul, 2018 09:02 PM

    David's Avatar

    Thanks, Brett. The preprocessor seems to crash every time there are non-ASCII characters. Any tip?

    Thank you again,
    d

  5. Support Staff 5 Posted by Brett on 16 Jul, 2018 03:41 PM

    Brett's Avatar

    You can force UTF-8 mode in recent versions of Ruby by changing the hash bang to this (first line of file):

    #!/usr/bin/env ruby
    # encoding: utf-8
    
    Encoding.default_external = Encoding::UTF_8
    Encoding.default_internal = Encoding::UTF_8
    

    For complete UTF-8 compatibility across Ruby versions, here's a full script:

    #!/usr/bin/env ruby
    # encoding: utf-8
    
    def class_exists?(class_name)
      klass = Module.const_get(class_name)
      return klass.is_a?(Class)
    rescue NameError
      return false
    end
    
    if class_exists? 'Encoding'
      Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?('default_external')
      Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?('default_internal')
    end
    
    begin
      input = STDIN.read.force_encoding('utf-8')
    rescue
      input = STDIN.read
    end
    
    $stdout.print input.gsub(/\[\[(.*?)\]\]/,'[\1](nvalt://find/\1)');
    
  6. 6 Posted by David Colmenare... on 16 Jul, 2018 03:59 PM

    David Colmenares's Avatar

    Hi Brett, thank you so much! Works like a charm out of the box!

    One last question, that you can of course ignore. Is there's a way to make Marked open the linked file? Clicking the wiki-link opens the file in Nvalt.

    Best,
    david

  7. Support Staff 7 Posted by Brett on 16 Jul, 2018 04:03 PM

    Brett's Avatar

    Well, the script is adding a link to nvalt://note_name, so it functions
    the way an nvALT wiki link would. If you want to link to the actual MD
    file, you'd want to change the `nvalt://` part to the path to the base
    notes folder, e.g. `file:///Users/your_user/Dropbox/nvALT/`. When you
    click one of those links, Marked will ask you how you want to handle it,
    and you can have it open in the same window, new window, or let Finder
    handle it. Note that opening in the same window does not give you a back
    button, it just completely replaces the current file.

    -Brett

  8. 8 Posted by David Colmenare... on 16 Jul, 2018 04:18 PM

    David Colmenares's Avatar

    The problem is that the link does not have an .md extension, so Marked can’t find the file...

  9. Support Staff 9 Posted by Brett on 16 Jul, 2018 04:29 PM

    Brett's Avatar

    Change this:

    $stdout.print input.gsub(/\[\[(.*?)\]\]/,'[\1](nvalt://find/\1)');
    

    to

    $stdout.print input.gsub(/\[\[(.*?)\]\]/,'[\1](file:///Users/[path/to/notes]/\1.md)');
    
  10. 10 Posted by David Colmenare... on 16 Jul, 2018 04:32 PM

    David Colmenares's Avatar

    Perfect! Thanks a ton!

    d

  11. Brett closed this discussion on 22 Jul, 2018 01:31 PM.

Comments are currently closed for this discussion. You can start a new one.

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