Markdown library?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
stam
Posts: 3060
Joined: Sun Jun 04, 2006 9:39 pm

Markdown library?

Post by stam » Mon Jul 08, 2024 1:51 pm

Hi all,

just checking if anyone is aware of a markdown library for formatting text in a LiveCode field?
I couldn't find one, so I'm guessing not, but maybe I missed it?

Many thanks
Stam

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Markdown library?

Post by dunbarx » Mon Jul 08, 2024 2:10 pm

Stam.

What is a markDown library? A table of some sort that relates text properties to this or that?

Craig

stam
Posts: 3060
Joined: Sun Jun 04, 2006 9:39 pm

Re: Markdown library?

Post by stam » Mon Jul 08, 2024 2:44 pm

markdown is a shorthand way of generating formatted text, typically used instead of html as it's less intrusive of the text provided.

For example, and <H1> header and some styles would be

Code: Select all

# This is a header
This word is in **bold**
This word is *italic*
instead of

Code: Select all

<H1>This is a header</H1>
This word is in <b>bold</b>
This word is <em>italic</em>
It just means you can write plain text with a few notations that are unobtrusive and it can be rendered in "proper" HTML.
Things like lists become a lot easier:

Code: Select all

- First item
- Second item
- Third item
    - Indented item
    - Indented item
- Fourth item
instead of

Code: Select all

<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item
    <ul>
      <li>Indented item</li>
      <li>Indented item</li>
    </ul>
  </li>
  <li>Fourth item</li>
</ul>
There's a ton of references to this online (and it's the defacto way of formatting GitHub documents).
Here's a simple reference: https://www.markdownguide.org/basic-syntax/

I guess a simple library wouldn't be enough, as you'd need a mode where the field produces the plain text with markdown notation and a different mode to show the rendered text.
But should be doable... so just wondering if someone might have already made the effort...

andresdt
Posts: 156
Joined: Fri Aug 16, 2019 7:51 pm
Contact:

Re: Markdown library?

Post by andresdt » Mon Jul 08, 2024 2:59 pm

Take a look at @trevordevore's gist and if you think it's useful, give him a star. :D
LiveCode Markdown converter: https://gist.github.com/trevordevore/5090459
Be kind, we all have our own wars.
https://torocruzand.com/

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: Markdown library?

Post by richmond62 » Mon Jul 08, 2024 3:26 pm

I guess a simple library wouldn't be enough, as you'd need a mode where the field produces the plain text with markdown notation and a different mode to show the rendered text.
I am not sure about that, as a 'standard' text import would bring plain text with markdown notation into a text field.

What would then be needed is a script (in, say, a button) to make that show up in another text field as rendered text.

Any script would "just' have to pop the text from the source file into a variable and "chew" its way along it through a load of conditional loops.

https://daringfireball.net/projects/markdown/

I went here: https://www.markdownguide.org/getting-started/

This made me feel a bit queer:

"One of the most confusing aspects of using Markdown is that practically every Markdown application implements a slightly different version of Markdown. These variants of Markdown are commonly referred to as flavors. It’s your job to master whatever flavor of Markdown your application has implemented."

I went here: https://macdown.uranusjr.com/

because I am currently working on a MacOS 12 iMac.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: Markdown library?

Post by richmond62 » Mon Jul 08, 2024 3:46 pm

The first thing I learnt (accidently) is that is you bung a '-' followed by a space in front of some text you end up with a 'bullet':
-
Screenshot 2024-07-08 at 17.41.06.png
-
Screenshot 2024-07-08 at 17.41.16.png
-
The ONLY problem about a script parsing this sort of thing would be HOW to differentiate between "Richmond's trans - sexual cousin" and "- transsexualism" where the second one is formatting for a bullet, and the second one is my personal problem, so were one looking for a "-" as a formatting mark one would have to look for a "-" preceded by a carriage return.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: Markdown library?

Post by richmond62 » Mon Jul 08, 2024 3:49 pm

And an underscore preceding a phrase followed by another underscore results in italicisation:
-
Screenshot 2024-07-08 at 17.46.45.png
-
Screenshot 2024-07-08 at 17.46.54.png
-
So, if you have the patience (and that is all it should take as this is not rocket science) you should be able to knock together a markdown decoder without too much difficulty.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: Markdown library?

Post by richmond62 » Mon Jul 08, 2024 3:54 pm

And so it goes:
-
Screenshot 2024-07-08 at 17.52.35.png
Screenshot 2024-07-08 at 17.52.35.png (20.65 KiB) Viewed 4902 times
-
Screenshot 2024-07-08 at 17.52.44.png
Screenshot 2024-07-08 at 17.52.44.png (9.76 KiB) Viewed 4902 times
-

And this was all that I required:

https://daringfireball.net/projects/markdown/syntax

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: Markdown library?

Post by richmond62 » Mon Jul 08, 2024 6:05 pm

Looking at markdown (which resembles HTML in some ways: all a bit confusing) I cannot help thinking a lot of things are redundant:

<table>
<tr>
<td>Let me introduce myself.</td>
<tr>
<table>

to produce:
-
Screenshot 2024-07-08 at 20.03.35.png
-
Seems clunky when, surely:

<table>Let me introduce myself.</table>

should suffice?

stam
Posts: 3060
Joined: Sun Jun 04, 2006 9:39 pm

Re: Markdown library?

Post by stam » Mon Jul 08, 2024 6:18 pm

andresdt wrote:
Mon Jul 08, 2024 2:59 pm
Take a look at @trevordevore's gist and if you think it's useful, give him a star. :D
LiveCode Markdown converter: https://gist.github.com/trevordevore/5090459
That's exactly what I was looking for, thank you @andresdt!
Haven't tested it yet, but I'm sure it will work fine (he says ;))

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Markdown library?

Post by FourthWorld » Mon Jul 08, 2024 6:32 pm

andresdt wrote:
Mon Jul 08, 2024 2:59 pm
Take a look at @trevordevore's gist and if you think it's useful, give him a star. :D
LiveCode Markdown converter: https://gist.github.com/trevordevore/5090459
Good find, thank you.

That library is described as "Converts a styledText array to Markdown".

Do you know of a library that goes the other way, converting Markdown to LC styledText arrays or htmlText?

I wrote an incomplete one about 20 years ago for a project, but Markdown has grown so much over the years and most other languages have lots of tools for these things, so hopefully our community does too.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

stam
Posts: 3060
Joined: Sun Jun 04, 2006 9:39 pm

Re: Markdown library?

Post by stam » Mon Jul 08, 2024 6:35 pm

richmond62 wrote:
Mon Jul 08, 2024 6:05 pm
Looking at markdown (which resembles HTML in some ways: all a bit confusing) I cannot help thinking a lot of things are redundant:

<table>
<tr>
<td>Let me introduce myself.</td>
<tr>
<table>
The first section you post is html. It's not markdown.
A typical markdown interpreter to produce the same result would take this as the text:

Code: Select all

|Let me introduce myself.|
|------------------------| // just 3 dashes needed to signify a row, these do not need to line up
richmond62 wrote:
Mon Jul 08, 2024 6:05 pm
<table>Let me introduce myself.</table>

should suffice?
if you're happy using malformed HTML sure. But as soon as you add anything else to the 'table' you'll have to do a large amount of work to make it function.

Markdown tables are much simpler than HTML tables and take the form:

Code: Select all

| column 1 header | column 2 header|
|-----------------|----------------|
| cell 1          | cell 2         |
Markdown offers some limited formatting options and of course, HTML is much more configurable. But this simple and quick and mostly is all that is needed.
EDIT: This is the GitHub flavour of markdown - John Gruber's initial creation does not include this functionality, but many flavours do

If you want to see what markdown tables look like have a look a wiki I'm creating on GitHub: https://github.com/stam66/regexPrimerFo ... characters

This is part of an intro to regex for LiveCode users I started writing mainly to clear up some more apocryphal stuff in my head, but reckon it may be useful to other LiveCoders... it's not yet complete but it's mostly there. I'll announce it properly when it is.

I don't know if Trevor's gist caters for this type of thing, but worth looking at.
Last edited by stam on Mon Jul 08, 2024 6:56 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: Markdown library?

Post by richmond62 » Mon Jul 08, 2024 6:51 pm

if you're happy using malformed HTML sure
Not particularly, but I am learning from https://daringfireball.net/projects/markdown/basics, and this is what happens:
-
Screenshot 2024-07-08 at 20.46.07.png
-
Obviously my app (MacDown) does not differentiate between HTML and markdown, and doing this:
-
Screenshot 2024-07-08 at 20.50.19.png
-
Does NOT produce the same result.

stam
Posts: 3060
Joined: Sun Jun 04, 2006 9:39 pm

Re: Markdown library?

Post by stam » Mon Jul 08, 2024 7:01 pm

richmond62 wrote:
Mon Jul 08, 2024 6:51 pm
Does NOT produce the same result.
Yes it does not. John Gruber was the initial inventor but in the last 20 years his format has been stagnant while many have added to this. I edited my post above to specifically mention that this is the GitHub dialect of markdown, and many, many markdown apps use this.

It's all shorthand for HTML - the underlying engine has to replace patterns and actually I see this as a potential role for regex.

Trevor's gist linked above coverts a styled text field's content to markdown and loops around 'style runs', it's not a full interpreter as it doesn't seem to do the opposite. A project for when I have more time maybe.

PS: I'm a long time fan of the daring fireball site. I bought some t-shirts to support them (pretty cool T's actually). One of the few RSS feeds I subscribe to.

But rather than using this antiquated reference, look at the opensource https://www.markdownguide.org
This is what is commonly used.

Post Reply