(M)ArkDown - 2023/04/20 - AHK v2.0.2

Post your working scripts, libraries and tools.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

(M)ArkDown - 2023/04/20 - AHK v2.0.2

04 May 2021, 13:01

This script is a markdown to HTML generator. It supports GitHub spec markdown, with a few additions that i wanted.

Please note that GitHub specific markdown, like commits and issue referencing, is not supported.

Markdown reference links/images are currently not supported, but adding support is on the To-Do list (still).

With this script function, you can enjoy an offline means to either preview your markdown, or just generate some web pages and/or documentation.

The below link was generated with this script, and is hosted on the same repo as the src script.

:arrow: Download on GitHub

==========================================================
Updates
==========================================================

To-Do List:
  • Add emojis?
Last edited by TheArkive on 20 Apr 2023, 10:50, edited 8 times in total.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2022/01/03 - beta.3

03 Jan 2022, 17:13

2022/01/03
  • fixed handling of NOT formatting text in <code> tags
  • added ***strong + emphesis*** formatting also ___strong+em___
  • cleaned up some examples in index.md
  • added an extra param to enforce some expected github formatting with normal markdown
  • added some styles for h1 - h6 hovering
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2022/01/09 - beta.3

09 Jan 2022, 15:04

2022/01/09
  • removed Static q := Chr(34) and used 'strings'
  • now when not specifying table header alignment, it takes the specified column alignment, per normal markdown spec
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2022/07/16 - beta.7

16 Jul 2022, 03:46

2022/07/16
  • more narrowly defined some regex to prevent infinite recursion in some cases
  • now formatting within headers is properly parsed
dahaiandtiankong
Posts: 2
Joined: 31 Oct 2022, 01:16

Re: (M)ArkDown - 2022/09/08 - beta.8

11 Nov 2022, 08:59

Hello.
My problem is like this, but is opposite.
when i collect some html text from web page, i want use a hotkey, convert html text to markdown text, then paste to markdown file.

I search whole board, and find nothing useful AHK script.
So can you give some advise? thanks.
neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: (M)ArkDown - 2022/09/08 - beta.8

11 Nov 2022, 13:40

dahaiandtiankong wrote:
11 Nov 2022, 08:59
when i collect some html text from web page, i want use a hotkey, convert html text to markdown text, then paste to markdown file.
Not an AutoHotkey script but the command line tool pandoc can convert from HTML to MD and to/from a lot of other different formats too. You can run it from AutoHotkey scripts and read and use its output.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2022/09/08 - beta.8

12 Nov 2022, 03:36

@dahaiandtiankong

I recommend the same as @neogna2 has said. I don't know of any such script that converts HTML to Markdown. Writing such a script would be tricky, depending on the src HTML.
dahaiandtiankong
Posts: 2
Joined: 31 Oct 2022, 01:16

Re: (M)ArkDown - 2022/09/08 - beta.8

12 Nov 2022, 07:28

Thank you answer my problem.
Yes, i know pandoc before, but i prefer use some simple way to solve this.

And today i find a script package named Turndown, it was writen in javascript, and can use with a simply way to convert html to markdown.
some code like this:

Code: Select all

var markdown = turndownService.turndown(document.getElementById('content'))
var markdown = turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>')
but it only can use in web development. what i want is some solution like this :P
Thanks again.
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: (M)ArkDown - 2022/09/08 - beta.8

11 Apr 2023, 23:48

Hi @TheArkive, I'm loving your library. I looked into a couple of other different approaches, but I've grown to really like this one. It covers quite a lot of code features while keeping the code size manageable.

I just wanted to report a couple results that I didn't expect. First, I ran the following code:
A_Clipboard := make_html('| A1 | B1 |`n|:----:|----|`n| A2 | B2 |', options, true)

The output was:
<table class="normal"><tr></tr><tr><th align="center">A1</th><th align="left">B1</th></tr><tr><td align="center">A2</td><td align="left">B2</td></tr></table>

I wasn't expecting the <tr></tr> before the header row. Is that meant to be there?

Also, here are a pair or inputs and the resulting outputs:

Code: Select all

 'this has *italic and **bold italic***' 
    ⇨ '<p>this has <em>italic and &lt;strong&gt;bold italic&lt;/strong&gt;</em></p>'
 'this has a **bold [link](http://google.com)**.' 
    ⇨  <p>this has a <strong>bold &lt;a href="http://google.com" target="_blank" rel="noopener noreferrer"&gt;link&lt;/a&gt;</strong>.</p>
My inclination would be to just remove the ltgt() calls from the sections of inline_code() dealing with 'emphasis,' 'strong,' and 'strong + emphasis.' Honestly, I like the ability to mix HTML and Markdown, so I want to be able to use occasional HTML within these inline formats. I'm quite happy to using the html character codes or a \ escape character for < and >. Just my personal preference, though.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2022/09/08 - beta.8

12 Apr 2023, 07:42

As for the empty <tr></tr>, I wouldn't say I intended that. I'll have to investigate.

I'm pretty sure I intended for usage of \ to allow escaping, but I don't recall how comprehensive I made this to be honest.

I'll have to take another look at it and fix some things.

I re-ran my test script and ended up seeing some artifacts that I did not intend, so it's time to revisit this anyway.
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: (M)ArkDown - 2022/09/08 - beta.8

12 Apr 2023, 13:16

Many thanks! I don't recall seeing the \ escapes in there yet, but hopefully they can be added near the end with a couple of strreplace()s.

For my on personal copy of the code, I just removed the ltgt() calls on lines 554, 564, 574, and 583. For me, that fixed everything except the table row issue. Hopefully, you won’t miss the functionality that those calls provided and hopefully the table row issue is equally easy to fix.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2022/09/08 - beta.8

13 Apr 2023, 00:29

One can't display a literal < or > and it needs to be written as it's corresponding entity:

https://www.w3schools.com/html/html_entities.asp

That is what ltgt() is for.

I just need to fix the parsing so that <strong> is properly written. Your fix certainly makes sense in the short term.
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: (M)ArkDown - 2022/09/08 - beta.8

13 Apr 2023, 08:07

Thanks for responding so quickly!

Yeah, I get that. Currently, your code does the following:

Code: Select all

make_html('`nAn <a href="google.com">inline link</a>.`n', options, true, false)
⇨ <p>An <a href="google.com">inline link</a>.</p>
AND
make_html('`nAn *<a href="google.com">inline link</a>*.`n', options, true, false)
⇨ <p>An <em>&lt;a href="google.com"&gt;inline link&lt;/a&gt;</em>.</p>
I absolutely love the first transformation. It is consistent both with John Gruber's spec and GFM and is a 'must-have' for me. In contrast, the second transformation seems to contradict Gruber's spec where he says that Span-level HTML tags can be used anywhere in a Markdown paragraph. It also seems to contradict the Github flavored markdown spec which says,
Text between < and > that looks like an HTML tag is parsed as a raw HTML tag and will be rendered in HTML without escaping.
* And I do confess, I'm writing this early in the morning and in a bit of a rush, so maybe I'm missing something.

As you know, inclusion of inline HTML is is an important part of Markdown. It is covered in detail in the "overview" section of John Gruber's original introduction to Markdown syntax. I suspect this is why you usually use the ltgt() function only within HTML attributes rather than more broadly, except when you are processing markdown inline formatting like emphasis and strong (ie lines, lines 554, 564, 574, and 583, which I mentioned above). I'm wondering if you added those 4 ltgt() calls in your 2022/01/03 update, because, unless I'm missing something they block all inline HTML within markdown inline markdown formatting. This contradicts when Gruber wrote,
Span-level HTML tags — e.g. <span>, <cite>, or <del> — can be used anywhere in a Markdown paragraph, list item, or header. If you want, you can even use HTML tags instead of Markdown formatting; e.g. if you’d prefer to use HTML <a> or <img> tags instead of Markdown’s link or image syntax, go right ahead.
Anyway, those are my two cents! I hope you find them helpful, but if not, no problem!
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2022/09/08 - beta.8

13 Apr 2023, 14:54

I honestly can't remember the details about that commit :P

The order of operations for parsing all this stuff properly is unfortunately quite tricky.

I've reviewed this script today and I'm planning some changes, mostly to be more inline with the GitHub specs for markdown.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2023/04/17 - AHK v2.0.2

17 Apr 2023, 04:25

2023/04/17
  • fixed parsing of *inline **markdown***
  • added true escaping with \
  • added line_height property to input options obj for custom text size (must be specified)
  • cleand up and condensed code
  • replaced some instances of stepping in with only calling inline_code()
  • updated index.md example
  • updated example CSS file
  • added color callouts according to the GitHub spec
  • added check lists
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: (M)ArkDown - 2023/04/17 - AHK v2.0.2

17 Apr 2023, 11:01

@TheArkive, thanks very much for this.
Regards,
burque505
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2023/04/17 - AHK v2.0.2

17 Apr 2023, 12:22

@burque505

I'm glad you find it useful :dance:
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: (M)ArkDown - 2023/04/17 - AHK v2.0.2

18 Apr 2023, 14:18

TheArkive wrote:
17 Apr 2023, 12:22
I'm glad you find it useful :dance:
Many thanks from me as well. I looked at the code and love the changes you've made. I definitely appreciate that raw HTML is fully available.

I've integrated the library into the script I use on a daily basis. Previously, I had started by ripping out 173 lines of code from _MD_gen.ahk that I new I wouldn't need (mostly related to <nav>, <toc>, and 'final'). This helped me learn the code better, but the more I got to know the code the more I liked it. It strikes a nice balance between including a massive amount of functionality without letting the codebase become overwhelming. At this point, I'm just including the code almost whole. On my computer, it only takes 8 milliseconds to parse your entire very complex 500 line index.md testfile.

Now that I've integrated your newer code into my script, I'll be using it almost every day. Thanks for the update!
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: (M)ArkDown - 2023/04/20 - AHK v2.0.2

20 Apr 2023, 10:51

2023/04/20
  • fixed list parsing: lists now display as they would on GitHub
  • added several customizing features for list customizing (offline markdown)
  • cleaned up example index.md
  • updated example css file

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: kunkel321, someguyinKC and 20 guests