github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/markup/blackfriday/renderer.go (about) 1 // Copyright 2019 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package blackfriday 15 16 import ( 17 "bytes" 18 "strings" 19 20 "github.com/russross/blackfriday" 21 ) 22 23 // hugoHTMLRenderer wraps a blackfriday.Renderer, typically a blackfriday.Html 24 // adding some custom behaviour. 25 type hugoHTMLRenderer struct { 26 c *blackfridayConverter 27 blackfriday.Renderer 28 } 29 30 // BlockCode renders a given text as a block of code. 31 // Chroma is used if it is setup to handle code fences. 32 func (r *hugoHTMLRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) { 33 if r.c.cfg.MarkupConfig.Highlight.CodeFences { 34 str := strings.Trim(string(text), "\n\r") 35 highlighted, _ := r.c.cfg.Highlight(str, lang, "") 36 out.WriteString(highlighted) 37 } else { 38 r.Renderer.BlockCode(out, text, lang) 39 } 40 } 41 42 // ListItem adds task list support to the Blackfriday renderer. 43 func (r *hugoHTMLRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) { 44 if !r.c.bf.TaskLists { 45 r.Renderer.ListItem(out, text, flags) 46 return 47 } 48 49 switch { 50 case bytes.HasPrefix(text, []byte("[ ] ")): 51 text = append([]byte(`<label><input type="checkbox" disabled class="task-list-item">`), text[3:]...) 52 text = append(text, []byte(`</label>`)...) 53 54 case bytes.HasPrefix(text, []byte("[x] ")) || bytes.HasPrefix(text, []byte("[X] ")): 55 text = append([]byte(`<label><input type="checkbox" checked disabled class="task-list-item">`), text[3:]...) 56 text = append(text, []byte(`</label>`)...) 57 } 58 59 r.Renderer.ListItem(out, text, flags) 60 } 61 62 // List adds task list support to the Blackfriday renderer. 63 func (r *hugoHTMLRenderer) List(out *bytes.Buffer, text func() bool, flags int) { 64 if !r.c.bf.TaskLists { 65 r.Renderer.List(out, text, flags) 66 return 67 } 68 marker := out.Len() 69 r.Renderer.List(out, text, flags) 70 if out.Len() > marker { 71 list := out.Bytes()[marker:] 72 if bytes.Contains(list, []byte("task-list-item")) { 73 // Find the index of the first >, it might be 3 or 4 depending on whether 74 // there is a new line at the start, but this is safer than just hardcoding it. 75 closingBracketIndex := bytes.Index(list, []byte(">")) 76 // Rewrite the buffer from the marker 77 out.Truncate(marker) 78 // Safely assuming closingBracketIndex won't be -1 since there is a list 79 // May be either dl, ul or ol 80 list := append(list[:closingBracketIndex], append([]byte(` class="task-list"`), list[closingBracketIndex:]...)...) 81 out.Write(list) 82 } 83 } 84 }