Skip to content

Instantly share code, notes, and snippets.

@trevordevore
Last active July 8, 2024 17:04
Show Gist options
  • Save trevordevore/5090459 to your computer and use it in GitHub Desktop.
Save trevordevore/5090459 to your computer and use it in GitHub Desktop.
LiveCode Markdown converter
/**
* \brief Converts a styledText array to Markdown.
*
* \param pTextA The styledText array to convert.
*
* Test while typing in a field:
* on textChanged
* put StyledTextToMarkdown(the styledText of me)
* pass textChanged
* end textChanged
* Test output at http://daringfireball.net/projects/markdown/dingus.
*
* \return Markdown
*/
function StyledTextToMarkdown pTextA
local i, j, thePara, theRun, theString, theStyles
set the wholematches to true
repeat with i = 1 to the number of elements of pTextA -- paragraphs
put empty into thePara
repeat with j = 1 to the number of elements of pTextA[i]["runs"] -- runs
if pTextA[i]["runs"][j] is not an array then next repeat
put empty into theRun
##########
# Get Text
##########
if "unicodetext" is among the keys of pTextA[i]["runs"][j] then
put unidecode(pTextA[i]["runs"][j]["unicodetext"], "utf8") into theRun
else
put unidecode(uniencode(pTextA[i]["runs"][j]["text"]), "utf8") into theRun
end if
put _EscapeMarkdownCharacters(theRun) into theRun
# <br />
replace numToChar(11) with space & space & cr in theRun
##########
## Define Styles
##########
put empty into theStyles
if pTextA[i]["runs"][j]["style"]["textColor"] is not empty then
put "color: rgb(" & pTextA[i]["runs"][j]["style"]["textColor"] & ")" into line (the number of lines of theStyles + 1) of theStyles
end if
if "underline" is among the items of pTextA[i]["runs"][j]["style"]["textStyle"] then
put "text-decoration:underline" into line (the number of lines of theStyles + 1) of theStyles
end if
##########
## Add links and styles
##########
if pTextA[i]["runs"][j]["style"]["linkText"] is not empty then
put unidecode(uniencode(pTextA[i]["runs"][j]["style"]["linkText"]), "utf8") into pTextA[i]["runs"][j]["style"]["linkText"]
put "[" before theRun
put "]" after theRun
put "(" & pTextA[i]["runs"][j]["style"]["linkText"] & ")" after theRun
end if
if "bold" is among the items of pTextA[i]["runs"][j]["style"]["textStyle"] then
put "**" before theRun
put "**" after theRun
end if
if "italic" is among the items of pTextA[i]["runs"][j]["style"]["textStyle"] then
put "*" before theRun
put "*" after theRun
end if
##########
## Add in spans for styles
##########
if theStyles is not empty then
replace cr with ";" in theStyles
## Since each line will get a new <p> tag then we need to apply span to each line.
put "<span style=" & quote & theStyles & quote & ">" before theRun
put "</span>" after theRun
end if
put theRun after thePara
end repeat
##########
## Wrap run in list
##########
if pTextA[i]["style"]["listStyle"] is not empty then
switch pTextA[i]["style"]["listStyle"]
case "disc"
case "circle"
case "square"
put "- " before thePara
break
case "skip"
put space & space & space & space before thePara
break
default
# numeric. Ignore listIndex for now. Markdown doesn't support starting lists at arbitrary number.
put "1. " before thePara
break
end switch
end if
put thePara after theString
if pTextA[i+1] is an array then
put cr after theString
end if
end repeat
return theString
end StyledTextToMarkdown
/**
* \brief Escapes markdown characters.
*
* \param pStr The string to escape.
*
* \return Esscaped string
*/
private function _EscapeMarkdownCharacters pStr
repeat for each char theChar in "\,`,*,_,{,},[,],(,),#,+,-,.,!"
replace theChar with "\" & theChar in pStr
end repeat
return pStr
end _EscapeMarkdownCharacters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment