Skip to content

Instantly share code, notes, and snippets.

@candelatech
Last active December 6, 2022 18:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save candelatech/15c62ac3ee019b8c50c2bbbd937a090f to your computer and use it in GitHub Desktop.
Save candelatech/15c62ac3ee019b8c50c2bbbd937a090f to your computer and use it in GitHub Desktop.
{% comment %}
Firstly, we can't comment inside liquid tags, so this turned into a mess pretty quickly.
In order to somewhat document this mess, I'll do my best without writing the comments inline :(
This snippet takes in a string variable called "raw", parses it for markdown, and spits out the
corresponding HTML representation
Currently, this only supports h1, h2, h3, strong, em, ul, and a
So something like this works just fine:
##Some Headline
###Some subheadline
**Lorem dolor** sit amet, consectetur adipiscing elit, **sed** do eiusmod tempor
ut labore [first one](https://www.google.com) et dolore magna aliqua.
Ut enim ad minim **veniam** [second](https://www.bing.com)
- Feature 1
- Feature 2
- Feature 3
The general logic here is simple for some elements, and complicated for others.
h1-h3 are simple, but don't support nesting behavior (i.e. can't put <strong> inside an <h1>)
The logic for p-tags is where it gets a little more convoluted.
Generally, the algorithm for inserting <strong> and <em> tags are the same, just a tag difference.
The <a>-tag-inserting function works a little differently since we need to rearrange the markup a little
after extracting the href and tag text values.
{% endcomment %}
{% liquid
assign lines = raw | newline_to_br | split: '<br />'
assign list_is_open = false
for line in lines
assign tag = ""
assign subline = line | strip
assign line_start = subline | slice: 0
assign line_is_list_item = false
comment
Skip this line if its just an empty newline tag
endcomment
if subline == "\n" or subline == ""
continue
endif
comment
H1 - H3 are simplistic
endcomment
if line contains "###"
assign tag = "h3"
elsif line contains "##"
assign tag = "h2"
elsif line contains "#"
assign tag = "h1"
elsif line_start == "-"
if list_is_open == false
echo "<ul>"
endif
assign line_is_list_item = true
assign list_is_open = true
assign tag = "li"
assign subline = subline | slice: 1,subline.size | strip
else
assign tag = "p"
if line contains "**"
assign full_subline = ""
assign subline_split = line | split:"**"
assign forloop_count = subline_split | size
for sub_split in subline_split
assign forloop_mod = forloop.index0 | modulo: 2
if forloop_mod == 0 and forloop.index != forloop_count
assign full_subline = full_subline | append: sub_split | append: "<strong>"
else
assign full_subline = full_subline | append: sub_split | append: "</strong>"
endif
endfor
assign subline = full_subline | replace:"**", ""
endif
if line contains "*"
assign full_subline = ""
assign subline_split = subline | split:"*"
assign forloop_count = subline_split | size
for sub_split in subline_split
assign forloop_mod = forloop.index0 | modulo: 2
if forloop_mod == 0 and forloop.index != forloop_count
assign full_subline = full_subline | append: sub_split | append: "<em>"
else
assign full_subline = full_subline | append: sub_split | append: "</em>"
endif
endfor
assign subline = full_subline | replace:"*", ""
endif
if line contains "]("
assign subline_split = subline | split:"["
assign forloop_count = subline_split | size
for sub_split in subline_split
if sub_split contains "]("
assign link_title = sub_split | split: "]("
assign link_title = link_title[0]
assign link_url = sub_split | split: "]("
assign link_url = link_url[1] | split: ")"
assign link_url = link_url[0]
assign real_link = "<a href='" | append: link_url | append:"'>" | append: link_title | append: "</a>"
assign to_replace = "[" | append: link_title | append: "](" | append:link_url | append:")"
assign subline = subline | replace: to_replace, real_link
endif
endfor
endif
endif
if line_is_list_item == false and list_is_open
echo "</ul>"
assign list_is_open = false
endif
assign clean_full_line = subline | replace:"#", ""
assign tag_start = "<" | append: tag | append: ">"
assign tag_end = "</" | append: tag | append: ">"
echo tag_start | append: clean_full_line | append: tag_end
endfor
%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment