tag:support.markedapp.com,2011-09-11:/discussions/questions/8916-previewing-without-double-square-bracketsMarked: Discussion 2022-03-27T11:25:45Ztag:support.markedapp.com,2011-09-11:Comment/422109832017-03-22T15:51:25Z2017-03-22T15:51:26ZPreviewing Without Double-square-brackets<div><p>I often use Marked2 to preview my nvalt notes. Nvalt notes are often littered with [[HyperLinkThis]]. That renders as ugly plain text in Marked2.</p>
<p>I'd like to set up Marked2 with a preprocessor, so that "[[HyperLinkThis]]" gets replaced with "<a>HyperLinkThis</a>".</p>
<p>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.</p>
<p>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?</p></div>Kevin Kleinfeltertag:support.markedapp.com,2011-09-11:Comment/422109832017-03-22T21:28:34Z2017-03-22T21:28:45ZPreviewing Without Double-square-brackets<div><p>This is possible with a basic preprocessor that does a regular<br>
expression substitution. Something like this in Ruby:</p>
<pre>
<code> #!/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])})"
}</code>
</pre>
<p>I can help you set that up if needed.</p>
<p>-Brett</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832017-03-23T22:48:19Z2017-03-23T22:48:22ZPreviewing Without Double-square-brackets<div><p>This is great. Thanks!</p></div>Kevin Kleinfeltertag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T16:27:11Z2021-05-21T16:27:12ZPreviewing Without Double-square-brackets<div><p>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.</p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T16:43:10Z2021-05-21T16:43:10ZPreviewing Without Double-square-brackets<div><p>@theredhype Yes, it needs to go in a .rb file, and you need to make it executable by running <code>chmod a+x path/to/file</code> 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.</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T17:03:47Z2021-05-21T17:03:50ZPreviewing Without Double-square-brackets<div><p>Thanks for the quick response.</p>
<p>I've now made the file executable, and confirmed by running <code>ls -l</code> which returns <code>-rwxr-xr-x</code> as expected.</p>
<p>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.</p>
<p>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.</p>
<p>Anything else I should check?</p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T17:26:09Z2021-05-21T17:26:09ZPreviewing Without Double-square-brackets<div><p>Do you have the pre-processor turned on in the preview window? At the<br>
bottom right side of the window there should be a yellow "light" if the<br>
preprocessor is enabled. (If "Automatically enable for new windows" is<br>
checked, this should be on by default when you open the preview).</p>
<p>If it's definitely running and you're still getting no log messages, try<br>
adding an <code>echo "TESTING"</code> line to the script and see if it shows up in<br>
the log. At least that would indicate it's running. The log will only<br>
update after a refresh, so be sure to hit ⌘R when testing.</p>
<p>-Brett</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T17:46:34Z2021-05-21T17:46:35ZPreviewing Without Double-square-brackets<div><p>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.</p>
<p>Still not quite there though. I'm getting the following error:</p>
<pre>
<code>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.</code>
</pre>
<p>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.</p>
<p>I checked my file permissions again, and they appear correct... I think. (ref. screenshot)<br>
`<code>-rwxr-xr-x@ 1 mrno staff 219 May 21 09:14 markedwikilinks.rb</code></p>
<p>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).</p>
<p>Do I need to Crossgrade?</p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T18:21:00Z2021-05-21T18:21:00ZPreviewing Without Double-square-brackets<div><p>Crossgrading would probably be the easiest solution. Another option to try is changing the processor to call ruby directly:</p>
<pre>
<code>Path: /usr/bin/ruby
Args: /Users/mrno/Documents/markedwikilinks.rb</code>
</pre>
<p>That <em>might</em> circumvent the executable permissions issue. No guarantees, sandboxing is complicated with shell scripts.</p>
<p>You can also try adding Marked to Full Disk Access in Security & Privacy if it isn't already.</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T18:27:18Z2021-05-21T18:27:21ZPreviewing Without Double-square-brackets<div><p>Thanks for confirming.<br>
Crossgrading has solved the issue.</p>
<p>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.</p>
<p>Thanks for all your help!</p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T18:40:02Z2021-05-21T18:40:03ZPreviewing Without Double-square-brackets<div><p>Should I be able to stay in a Marked 2 window, and navigate between file previews, perhaps by changing the <code>nvalt://</code> portion of your ruby code at the top of this file to something like <code>Marked+2://</code> ?</p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T21:41:26Z2021-05-21T21:41:26ZPreviewing Without Double-square-brackets<div><p>Yep, the only difference between versions is the sandboxing, which means<br>
the direct version is actually slightly more capable than the MAS<br>
version.</p>
<p>As far as previewing links within Marked, if you change the links to<br>
just be file:// urls to markdown files, you can open them in the current<br>
window or a new window, you should get an option. Marked doesn't have a<br>
back button, though, so opening in the same window is a one-way street.<br>
You can also make them <code>x-marked://open/?file=...</code> links to force them<br>
to open new windows.</p>
<p>-Brett</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-21T23:05:22Z2021-05-21T23:05:23ZPreviewing Without Double-square-brackets<div><p>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</p>
<p>I've got that working nicely by using a hard path to the folder I'm in, like this:</p>
<pre>
<code>#!/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])})"
}</code>
</pre>
<p>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.</p>
<p>Is it possible?</p>
<p>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.</p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-22T16:31:48Z2021-05-22T16:31:48ZPreviewing Without Double-square-brackets<div><p>Ok, let me revise my suggestion. You don't need the <code>file://</code> 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 <code>.md</code> if it's not already there, which you might need to adjust if your default file extension isn't <code>.md</code>.</p>
<pre>
<code>#!/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</code>
</pre></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-23T01:35:02Z2021-05-23T01:35:03ZPreviewing Without Double-square-brackets<div><p>Happy to report file names with spaces are working well! Thanks for noticing that.</p>
<p>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.</p>
<p>If I add the <code>file:///Users/user/Documents/folder/</code> 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.</p>
<p>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.</p>
<p>Note: In my .rb file, I did swap out the <code>do</code> and <code>end</code> from the above code block for brackets, otherwise Marked 2 was simply displaying <code><Enumerator:0x00007fa233094ea8></code></p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-23T19:36:28Z2021-05-23T19:36:28ZPreviewing Without Double-square-brackets<div><p>What OS version are you on? The version of the script above was tested<br>
and working fine on Big Sur with the bundled Ruby. Bracket syntax and<br>
do/end are just two variations of the same block syntax, there shouldn't<br>
be any operational difference between the two.</p>
<p>But anyway, since that's working now, right click on a link showing<br>
Missing File and choose Inspect. Tell me what the actual URL it shows in<br>
the link markup is. As long as it doesn't have any prefix and is just<br>
the filename, it should work. The only thing I can think to double check<br>
is that the actual file has the same <code>.md</code> extension the link is looking<br>
for...</p>
<p>-Brett</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-24T06:52:21Z2021-05-24T06:52:23ZPreviewing Without Double-square-brackets<div><p>I'm on macOS Catalina 10.15.7 (19H1030)</p>
<p>ref. attached screenshot — inspect appears to reveal the URL is simply the filename?</p>
<p>Maybe you'll notice something in the screenshot that I'm not.</p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-24T11:31:54Z2021-05-24T11:31:54ZPreviewing Without Double-square-brackets<div><p>Yep, that's exactly what it should be. Now assure me that in the same<br>
nvUltra folder there actually is a file named "Founder.md", including<br>
the same capitalization.</p>
<p>-Brett</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-24T23:39:12Z2021-05-24T23:39:13ZPreviewing Without Double-square-brackets<div><p>Yes, it's in the same folder and named precisely the same.</p>
<p>Brett! I think I've found a big clue to the mystery.</p>
<p>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.</p>
<p>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!</p>
<p>Then, when I switch Marked 2 back to watching the parent, those files go missing.</p>
<p>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.</p>
<p>But I'm guessing, if I've found the issue, this is something I can't fix on my end.</p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-25T10:16:08Z2021-05-25T10:16:08ZPreviewing Without Double-square-brackets<div><p>Yep, in a folder preview Marked treats all paths as if they were in the<br>
base folder, so it's not looking in subfolders for relative paths. I<br>
should probably change that, but I actually forgot that Marked could<br>
even read subfolders in folder view mode.</p>
<p>I think that the MARKED_ORIGIN environment variable should be correct,<br>
though, so it would be simple to adjust the preprocessor to include an<br>
absolute (or more correct relative) path. Will have to test, though.</p>
<p>-Brett</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-25T11:23:25Z2021-05-25T11:23:25ZPreviewing Without Double-square-brackets<div><p>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.</p>
<pre>
<code>#!/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'))})"
}</code>
</pre></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-25T19:45:24Z2021-05-25T19:48:17ZPreviewing Without Double-square-brackets<div><p>Brilliant. Thank you Brett! The relative file paths are now working for me. I can use the same preprocessor for any path / folder.</p>
<p>I think I've found one use case that is still breaking. Steps to reproduce:</p>
<ol>
<li>Open Marked 2, point it at top level folder which contains folders of .md docs.<br></li>
<li>Open two or more lower level folders as tabs in nvUltra.<br></li>
<li>In Marked 2, click on a wiki link and successfully navigate to another doc preview<br></li>
<li>In nvUltra, edit any doc at any level in the directory being watched<br></li>
<li>Result: ref screenshot: <a href="https://i.imgur.com/iV1ToYk.png">No Readable File Found</a><br></li>
</ol>
<p>Notes</p>
<ul>
<li>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.</li>
<li>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).</li>
</ul></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-25T21:30:44Z2021-05-25T21:30:44ZPreviewing Without Double-square-brackets<div><p>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.</p></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832021-05-25T22:38:05Z2021-05-25T22:38:06ZPreviewing Without Double-square-brackets<div><p>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!</p>
<p>I tried this with <code>[Parent](file:///Users/User/Dropbox/nvUltra)</code> 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.</p>
<p>Not sure whether I'd add this to my workflow. But it's an interesting workaround.</p></div>Michaeltag:support.markedapp.com,2011-09-11:Comment/422109832021-05-25T23:28:55Z2021-05-25T23:28:55ZPreviewing Without Double-square-brackets<div><p>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.</p>
<ul>
<li>Brett</li>
</ul></div>Bretttag:support.markedapp.com,2011-09-11:Comment/422109832022-03-26T22:51:30Z2022-03-26T22:51:36ZPreviewing Without Double-square-brackets<div><p>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.</p></div>danieltag:support.markedapp.com,2011-09-11:Comment/422109832022-03-26T23:07:24Z2022-03-26T23:07:26ZPreviewing Without Double-square-brackets<div><p>For clarity I should add that I want the Wiki Link to open in Marked 2 (not the other app).</p></div>danieltag:support.markedapp.com,2011-09-11:Comment/422109832022-03-27T11:25:36Z2022-03-27T11:25:36ZPreviewing Without Double-square-brackets<div><p>@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.</p></div>Brett