How to make a simple markdown system?

Hey developers!

Is there any way to make a basic markdown system (bold, italic, strikethrough, monospace, headers, list, blockquote)? With that, I mean detecting the triggering characters instead of making the actual GUI’s.
I’m not really interested in those complicated modules. If that is the only way to go, then I’ll just forget about it.
I was thinking about using RichText for this. Something like:

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
script.Parent.Text = string.gsub(script.Parent.Text, "*", "<b>")
end)

But of course the first * needs to be <b>, and the second * needs to be </b>. So I’m not sure if that is a possibility.
Any help would be appreciated!

Have a nice day!

I have never tried this myself, but I assume you could count and find the number of chars (such as *) in a phrase, then you could modify it to be rich text using gsub. There would have to be a little bit of string processing because if you wanted to have multiple sets of say bolded text, you’d have to find when you had begun and ended a bolded phrase, then scan the string for the next set of bolded text.

1 Like

I don’t really know that much about strings, how would I find the first and last character?

string.gmatch() might help you, but I haven’t tried it yet.
For example:

local i = 0
for word in string.gmatch('*someText*', '*') do
 i += 1
 if i % 2 == 0 then
  -- convert the word to <b>
 else
  -- convert the word to </b>
 end
end

I don’t actually know how to replace certain words in strings, so that’s up to you to figure out.

1 Like

Should *someText* be replaced by script.Parent.Text?

Yes
(why does roblox hate replies with only one word?!)

1 Like

People be like:
30303030303030303030303030303030303030303030303030303030303030303030303030303030303

1 Like

Let’s say you have the string I want to go to the movies with my friend **Joe** and **Suzie**. Joe is supposed to be bolded, as is Suzie.

What I would do is traverse the string and find the first character that matches your intended richtext equivalent, then append it to <b> or whatever (add to a variable one to indicate that the first part of the intended bold has been found), locate the second character (add again to that previously defined variable), when you have finished finding the first section of bolded text, if that variable is divisible by 2, then the first
word “Joe”, can successfully be bolded. Then continue to find the bolded characters in front of and behind the word “Suzie”, if that number (added to the previous variable whenever you find one) is still divisible by 2, Suzie can be bold!

local stringToSearch = script.Parent.Text
local numberOfInstances = 0
local occurence = 0
local startBold = "<b>"
local endBold = "<\b>"
script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	for character in stringToSearch
		if numberOfInstances ~= 0 and not numberOfInstances / 2 then
			if string.find(stringToSearch, "**", occurence) then
				occurence = string.find(stringToSearch, "**")
				numberOfInstances += 1
				---replace characters here
		else if numberOfInstances = 0 then
			break 
end)

Take this with a grain of salt, because my mind is steeped in Java code right now and I didn’t actually get to test this, and like comsurg, I’m not sure how exactly to append characters into the string with gsub at specific occurences (or you could split the string and reappend it afterwards buts thats real complicated) unless you were to simply use like * to signal the start and ** to signal the end of a bold or whatnot. Nevertheless, this code should be used as a structure rather than actual program code.

EDIT: My brain is real confused right now, you should actually be looping through and finding the # of occurences with indices before the loop in the above code because if you do not it will keep searching because of the divisible by 2 statement and the numberOfInstances ~= 2

string.find should find the first index that ** occurs at and then you can append it from there, my loop is really bad so it will absolutely have to be changed, so just stick with my text explanation for now rather than my code. :sweat_smile:

1 Like

Thanks for your amazing explanation and help! I will try this in a minute.

1 Like