Skip to content

Commit

Permalink
regex to render segments of diff
Browse files Browse the repository at this point in the history
  • Loading branch information
calaldees committed Nov 15, 2024
1 parent 48ae319 commit c0025df
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions teachprogramming/lib/static/projects.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,22 @@ <h1>Projects</h1>
]).flat())
}

const REGEX_DIFF_SEGMENT = /@@ (?<back>[-+]\d+,\d+) (?<forward>[-+]\d+,\d+) @@\n(?<code>.*?)(?=$|\n@@)/sg
function renderDiff(diff) {
if (!diff) {return}
return h('code',{},diff.split("\n").map(line=>[
h('span',{className:"lineNum"},"100 "),
h('span',{className:[
"line",
line.startsWith("+")?"add":"",
line.startsWith("-")?"remove":"",
].filter(x=>x).join(" ")},line+"\n"),
]).flat())
//for (let dd of diff.matchAll(REGEX_DIFF_SEGMENT)) {}
function renderDiffSub(code_segment, lineNums) {
console.log(lineNums)
return h('code',{},code_segment.split("\n").map(line=>[
h('span',{className:"lineNum"},"100 "),
h('span',{className:[
"line",
line.startsWith("+")?"add":"",
line.startsWith("-")?"remove":"",
].filter(x=>x).join(" ")},line+"\n"),
]).flat())
}
return [...diff.matchAll(REGEX_DIFF_SEGMENT)].map(match=>renderDiffSub(match.groups.code, match.groups.forward))
}

// Consider CSS toggle
Expand Down

0 comments on commit c0025df

Please sign in to comment.