back interdiff of round #1 and #0

improve code blocks in markdown (syntax highlighting and copy-to-clipboard button) #145

closed
opened by gwen.works targeting master from gwen.works/core: copycodeblocks

right now the button's text gets added without the around it. can't figure out why. am i missing sth obvious ?

ERROR
go.mod

Failed to calculate interdiff for this file.

ERROR
go.sum

Failed to calculate interdiff for this file.

NEW
appview/pages/funcmap.go
··· 145 145 }, 146 146 "markdown": func(text string) template.HTML { 147 147 rctx := &markup.RenderContext{RendererType: markup.RendererTypeDefault} 148 - return template.HTML(bluemonday.UGCPolicy().Sanitize(rctx.RenderMarkdown(text))) 148 + return template.HTML( 149 + rctx.RenderMarkdown(text, bluemonday.UGCPolicy().Sanitize), 150 + ) 149 151 }, 150 152 "isNil": func(t any) bool { 151 153 // returns false for other "zero" values
NEW
appview/pages/pages.go
··· 432 432 ext := filepath.Ext(params.ReadmeFileName) 433 433 switch ext { 434 434 case ".md", ".markdown", ".mdown", ".mkdn", ".mkd": 435 - htmlString = p.rctx.RenderMarkdown(params.Readme) 435 + htmlString = p.rctx.RenderMarkdown(params.Readme, p.rctx.Sanitize) 436 436 params.Raw = false 437 - params.HTMLReadme = template.HTML(p.rctx.Sanitize(htmlString)) 437 + params.HTMLReadme = template.HTML(htmlString) 438 438 default: 439 439 htmlString = string(params.Readme) 440 440 params.Raw = true ··· 564 564 case markup.FormatMarkdown: 565 565 p.rctx.RepoInfo = params.RepoInfo 566 566 p.rctx.RendererType = markup.RendererTypeRepoMarkdown 567 - htmlString := p.rctx.RenderMarkdown(params.Contents) 568 - params.RenderedContents = template.HTML(p.rctx.Sanitize(htmlString)) 567 + htmlString := p.rctx.RenderMarkdown(params.Contents, p.rctx.Sanitize) 568 + params.RenderedContents = template.HTML(htmlString) 569 569 } 570 570 } 571 571
NEW
input.css
··· 104 104 } 105 105 } 106 106 107 + /* Hidden elements */ 108 + [aria-hidden="true"] { 109 + display: none ; 110 + } 111 + 107 112 /* Background */ 108 113 .bg { 109 114 color: #4c4f69;
NEW
appview/pages/markup/markdown.go
··· 20 20 "github.com/yuin/goldmark/text" 21 21 "github.com/yuin/goldmark/util" 22 22 htmlparse "golang.org/x/net/html" 23 + "golang.org/x/net/html/atom" 23 24 24 25 "tangled.sh/tangled.sh/core/appview/pages/repoinfo" 25 26 ) ··· 153 154 } 154 155 } 155 156 157 + if node.Data == "pre" { 158 + // TODO only show when :hover or :focus on the pre element 159 + button := &htmlparse.Node{ 160 + Type: htmlparse.ElementNode, 161 + DataAtom: atom.Button, 162 + Data: "button", 163 + Attr: []htmlparse.Attribute{ 164 + { 165 + Key: "class", 166 + Val: "absolute top-2 right-2 btn", 167 + }, 168 + { 169 + Key: "style", 170 + // FIXME .#watch-tailwind doesnt seem to catch top-2 and right-2, probably cuz it's not used anywhere inside of templates/ ? 171 + Val: "top: 0.5rem; right: 0.5rem;", 172 + }, 173 + { 174 + Key: "onclick", 175 + Val: ` 176 + navigator.clipboard.writeText(this.closest('pre').querySelector('code').innerText); 177 + this.innerText = 'Copied!'; 178 + setTimeout(() => { this.innerText = 'Copy' }, 1500); 179 + `, 180 + }, 181 + { 182 + Key: "onload", 183 + Val: "this.removeAttribute('aria-hidden')", 184 + }, 185 + { 186 + Key: "aria-hidden", 187 + Val: "true", 188 + }, 189 + { 190 + Key: "title", 191 + Val: "Copy to clipboard", 192 + }, 193 + }, 194 + } 195 + 196 + // TODO 197 + // if copyIcon, err := icons.IconNode("copy", "h-4", "w-4"); err != nil { 198 + // button.AppendChild(copyIcon) 199 + // } else { 200 + button.AppendChild(&htmlparse.Node{ 201 + Type: htmlparse.TextNode, 202 + Data: "Copy", 203 + }) 204 + 205 + var classWasSetOnNode bool 206 + for i, attr := range node.Attr { 207 + if attr.Key == "class" { 208 + node.Attr[i].Val += " relative" 209 + classWasSetOnNode = true 210 + break 211 + } 212 + } 213 + 214 + if !classWasSetOnNode { 215 + node.Attr = append(node.Attr, htmlparse.Attribute{ 216 + Key: "class", 217 + Val: "relative", 218 + }) 219 + } 220 + 221 + node.AppendChild(button) 222 + } 223 + 156 224 for n := node.FirstChild; n != nil; n = n.NextSibling { 157 225 visitNode(ctx, n) 158 226 }