Jump to content

Is there an way I can convert a markdown file of hyperlink into a script filter output?


nikivi

Recommended Posts

I have this file (https://transfer.sh/mIk6a/research.md)

 

Each line of it is either a hyperlink or just some text. 

 

Is it possible to have a quick way that will taken this markdown file, create a json struct for alfred script filter where the title is each of the line's text and the action of it is the url attached to the text?

 

The output will then look like this (with all of the fields transferred in this way ): 

 

vqPx0x4.png 

 

I looked into parsing this markdown file into a json struct (https://github.com/njvack/markdown-to-json) but that doesn't solve the issue as then I still need to have a correct json. I guess the only solution is to write some kind of custom markdown parser suited for this specific problem? Or perhaps there is some other way I can go to solve this?

 

Thank you for any help.

Edited by nikivi
Link to comment

Make this workflow:

s8aytZk.png

 

Make the Script Filter code:

IFS=$'\n'

echo "<?xml version='1.0'?><items>"
for line in $(grep '\[.+\]\(.+\)' < 'FILE_PATH'); do
  sed -E "s|.*\[(.+)\]\((.+)\).*|<item arg='\2' valid='yes'><title>\1</title></item>|" <<< "${line}"
done
echo "</items>"

Replace FILE_PATH with the actual path to your file, and you’re done. Activate Alfred filters results as well, and you’ll get filtering for free.

Edited by vitor
Link to comment

For some reason, I only saw this now. :(

 

Thank you so much Vitor for this. I have however replaced the file path but it doesn't work. The file is on my Desktop.

 

So I filled in the script filter like so : 

 

CtPyLUD.png 

 

I tried '~/Desktop/research' without the extension too.

 

Also I wanted to create a workflow for sharing so anyone could use it and not having to read things from some file every time you fire up a workflow.

 

I guess you can echo it to some file in the alfred workflow directory and then read that file. Would this work or there is a better way to do it?

 

Thank you a lot Vitor for this. This is such a time saving workflow as I very often share links to the mind maps I have made and I think it would be nice to have an Alfred interface to all my mind maps so anyone can use them and use them fast.

 

 

Link to comment

I forgot a flag in the original code. The correct one is

IFS=$'\n'

echo "<?xml version='1.0'?><items>"
for line in $(grep --extended-regexp '\[.+\]\(.+\)' < "${HOME}/Desktop/research.md"); do
  sed -E "s|.*\[(.+)\]\((.+)\).*|<item arg='\2' valid='yes'><title>\1</title></item>|" <<< "${line}"
done
echo "</items>"

 

1 hour ago, nikivi said:

Sadly the transfer.sh file action workflow you shared doesn't work on multiple files.

 

I know, that’s by design. I do zip directories, though, so I’ll consider zipping multiple files together as well.

Link to comment
  • 1 month later...

I am trying to create a new workflow which would parse the markdown file and take into the account the nesting of headers like I outline here.

 

I am curious, would it be possible to add the annotations that I have below the headers and add them as subtitles to the 'script filter item'? So I will get something like this as result : 

 

UKhceHn.png 

 

And if I press some modifier key, it would show me the subtitle of the item which will be the full description that is attached to the link. This one : 

 

ldE5oF0.png 

 

Here is the markdown file of links and descriptions.

 

Edited by nikivi
Link to comment
  • 4 years later...
On 2/25/2017 at 10:34 AM, vitor said:

I forgot a flag in the original code. The correct one is

IFS=$'\n'

echo "<?xml version='1.0'?><items>"
for line in $(grep --extended-regexp '\[.+\]\(.+\)' < "${HOME}/Desktop/research.md"); do
  sed -E "s|.*\[(.+)\]\((.+)\).*|<item arg='\2' valid='yes'><title>\1</title></item>|" <<< "${line}"
done
echo "</items>"

 

 

I know, that’s by design. I do zip directories, though, so I’ll consider zipping multiple files together as well.

 

Thanks a lot for this! I use it often. How might I go about modifying it so that it works with uri schemes instead of http links? Running it as is on links like the one below doesn't seem to show them as expected.

 

- [Home](obsidian://advanced-uri?vault=Obsidian&workspace=home)
- [Projects](obsidian://advanced-uri?vault=Obsidian&workspace=projects)

- [Work](obsidian://advanced-uri?vault=Obsidian&workspace=work)

 

Running it with standard weblinks works fine though

 

- [Google](https://google.ca)
- [Alfred](https://alfredforum.com)

 

Any thoughts?

Link to comment
8 hours ago, brapstallion said:

doesn't seem to show them as expected

 

What do you mean "[not] as expected"? What are you getting instead?

 

Please be precise when describing errors. It's a lot easier to figure out what's gone wrong when you can see the incorrect result, and don't just have a vague description like "it doesn't work" or "not as expected".

Link to comment
On 9/2/2021 at 4:08 AM, deanishe said:

 

What do you mean "[not] as expected"? What are you getting instead?

 

Please be precise when describing errors. It's a lot easier to figure out what's gone wrong when you can see the incorrect result, and don't just have a vague description like "it doesn't work" or "not as expected".


I've added Victor's code to a script filter and configured it by changing to the path "${HOME)/Desktop/Research.md" to one containing my own list of Markdown formatted links. The file is are formatted like so:

 

- [Google](https://google.ca)

- [Alfred](https://alfredforum.com)

 

When running the workflow using this configuration, I expect it to read the markdown file, parse content between [ ] for script filter item titles, and parse content between ( ) for script filter item arguments. When selecting a script filter item, the argument "https://alfredforum.com" is passed to an Open URL object and the alfred forum is opened in my browser. This is the expected behaviour.

 

When using the same setup but changing markdown list to contain uri schemes like "[Home](obsidian://advanced-uri?vault=Obsidian&workspace=home)" instead of web links like "[Alfred](https://alfredforum.com)" the workflow does not behave as expected. Instead, no items are shown in alfred's script filter.

 

Is that clearer?

Link to comment
1 hour ago, brapstallion said:

Is that clearer?

 

Not really, I'm afraid. What you're doing is clear enough, it's your description of what happens when you run the script that isn't.

 

1 hour ago, brapstallion said:

no items are shown in alfred's script filter.

 

That's just another way of saying "nothing happens". That is not an actionable description.

 

Something is happening. Open the debugger and check for an error message. If there isn't one, switch to "All information" and check the workflow's XML output.

 

Off the top of my head, I’d guess it’s an escaping error: & isn’t allowed in XML. It needs to be replaced with &amp;.

Edited by deanishe
Link to comment
2 hours ago, deanishe said:

 

Not really, I'm afraid. What you're doing is clear enough, it's your description of what happens when you run the script that isn't.

 

 

That's just another way of saying "nothing happens". That is not an actionable description.

 

Something is happening. Open the debugger and check for an error message. If there isn't one, switch to "All information" and check the workflow's XML output.

 

Off the top of my head, I’d guess it’s an escaping error: & isn’t allowed in XML. It needs to be replaced with &amp;.

 

 

ah ok. this is what the debugger says;

 

[20:24:45.590] test[Script Filter] Queuing argument ' '
[20:24:45.636] test[Script Filter] Script with argv ' ' finished
[20:24:45.640] test[Script Filter] <?xml version='1.0'?><items>
<item arg='obsidian://advanced-uri?vault=Deadbone&workspace=projects%20all' valid='yes'><title>All</title></item>
<item arg='obsidian://advanced-uri?vault=Deadbone&workspace=projects%20proj1' valid='yes'><title>Project 1</title></item>
<item arg='obsidian://advanced-uri?vault=Deadbone&workspace=projects%20proj2' valid='yes'><title>Project 2</title></item>
<item arg='obsidian://advanced-uri?vault=Deadbone&workspace=projects%20proj3' valid='yes'><title>Project 3</title></item>
</items>
[20:24:45.641] WARNING: test[Script Filter] XML Parse Error 'The operation couldn’t be completed. (NSXMLParserErrorDomain error 23.)'. Row 2, Col 60: 'EntityRef: expecting ';'' in XML:
<?xml version='1.0'?><items>
<item arg='obsidian://advanced-uri?vault=Deadbone&workspace=projects%20all' valid='yes'><title>All</title></item>
<item arg='obsidian://advanced-uri?vault=Deadbone&workspace=projects%20proj1' valid='yes'><title>Project 1</title></item>
<item arg='obsidian://advanced-uri?vault=Deadbone&workspace=projects%20proj2' valid='yes'><title>Project 2</title></item>
<item arg='obsidian://advanced-uri?vault=Deadbone&workspace=projects%20proj3' valid='yes'><title>Project 3</title></item>
</items>

 

Link to comment
14 hours ago, deanishe said:

Yup. It’s the & symbols making the XML invalid.

 

The simple solution is to change & to &amp; in the URLs in your Markdown document(s).

 

The proper solution is to use a real XML library that guarantees valid output. (And a real Markdown parser, too, ideally.)

 

nice one. that worked! thank you!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...