Rich text problem

I am working on scripting plugin but I’ve encountered an issue while making script diff function.
I’m using rich text to mark the differences but it doesn’t work every time.

                        local display : string = ""
						if source and content then
							if source == content then
								local i = 1
								for line in localRepo.scriptDocuments[currentFile].Source:gmatch("[^\n]+") do
									display..= "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. " " .. line .. "\n"
									i = i + 1
								end
								pageModule.currentPages.repository.file.ScrollingFrame.code.Text = display
							else
								local function compareStrings(oldString, newString)
									local oldLines = {}
									local newLines = {}
									local comparedLines = ""
									for line in oldString:gmatch("[^\n]+") do
										table.insert(oldLines, line)
									end;
									for line in newString:gmatch("[^\n]+") do
										table.insert(newLines, line)
									end;
									local max = if #oldLines > #newLines then #oldLines else #newLines;
									for i = 1, max do
										if oldLines[i] ~= newLines[i] then
											if not (i > #oldLines) and not (i > #newLines) then
												comparedLines..= "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. " " .. "<mark color='#006702' transparency='0'>" .. "+" .. newLines[i] .. "</mark>" .. " " .. "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. " " .. "<mark color='#9B0000' transparency='0'>" .. "-" .. " " .. oldLines[i] .. "</mark>" .. "\n"
											end;
											if i > #oldLines then
												comparedLines..= "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. " " .. "<mark color='#006702' transparency='0'>" .. "+" .. " " .. newLines[i].. "</mark>" .. " " .. "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. "\n"
												--comparedLines..= "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. "\n"
											elseif i > #newLines then
												comparedLines..= "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. " " .. "<mark color='#9B0000' transparency='0'>" .. "-" .. " " .. oldLines[i] .. "</mark>" .. " " .. "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. "\n"
												--comparedLines..= "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. "\n"
											end;
										elseif oldLines[i] == newLines[i] then
											comparedLines..= "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. " " .. newLines[i] .. "\n"
											--comparedLines..= "<mark color='#141414' transparency='0'>" .. i .. "</mark>" .. "\n"	
										end;
									end
									return comparedLines
								end
								task.wait()
								pageModule.currentPages.repository.file.ScrollingFrame.code.Text = compareStrings(content, source)
							end
						end

it is part of a bigger script ≈800 lines if the rest of the code is needed I’ll provide it,

The function at the top (used when both compared scripts are the same) works fine

but the other function used to compare scripts doesn’t alway work (only managed to see the right result once)

The html tags just don’t render in this case.

1 Like

That becouse you need to turn some symbols into character escapes

local escape:{[string]:string} = {
    ['&'] = '&amp;',
    ['<'] = '&lt;',
    ['>'] = '&gt;',
    ['"'] = '&quot;',
    ['\''] = '&apos;',
}
local function WriteRichText(str:string,color:Color3):string
    return `<font color="#{color:ToHex()}">{string.gsub(str,`[&<>"\']`,escape)}</font>`
end
3 Likes

Thanks a bunch!

I was completely stumped trying to figure this out, but I totally forgot that I’m comparing scripts that have these characters in them. Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.