...

Source file src/oss.terrastruct.com/d2/d2renderers/d2svg/code.go

Documentation: oss.terrastruct.com/d2/d2renderers/d2svg

     1  package d2svg
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/alecthomas/chroma/v2"
     7  	"github.com/alecthomas/chroma/v2/formatters/svg"
     8  )
     9  
    10  // Copied private functions from chroma. Their public functions do too much (write the whole SVG document)
    11  // https://github.com/alecthomas/chroma
    12  // >>> BEGIN
    13  
    14  var svgEscaper = strings.NewReplacer(
    15  	`&`, "&",
    16  	`<`, "&lt;",
    17  	`>`, "&gt;",
    18  	`"`, "&quot;",
    19  	` `, "&#160;",
    20  	`	`, "&#160;&#160;&#160;&#160;",
    21  )
    22  
    23  func styleToSVG(style *chroma.Style) map[chroma.TokenType]string {
    24  	converted := map[chroma.TokenType]string{}
    25  	// NOTE this is in the original source code, but it just makes unhighlightable code turn into the bg color
    26  	// Which I don't understand, and I get the results I want when I remove it.
    27  	// bg := style.Get(chroma.Background)
    28  	for t := range chroma.StandardTypes {
    29  		entry := style.Get(t)
    30  		// if t != chroma.Background {
    31  		//   entry = entry.Sub(bg)
    32  		// }
    33  		if entry.IsZero() {
    34  			continue
    35  		}
    36  		converted[t] = svg.StyleEntryToSVG(entry)
    37  	}
    38  	return converted
    39  }
    40  
    41  func styleAttr(styles map[chroma.TokenType]string, tt chroma.TokenType) string {
    42  	if _, ok := styles[tt]; !ok {
    43  		tt = tt.SubCategory()
    44  		if _, ok := styles[tt]; !ok {
    45  			tt = tt.Category()
    46  			if _, ok := styles[tt]; !ok {
    47  				return ""
    48  			}
    49  		}
    50  	}
    51  	// Custom code
    52  	out := strings.Replace(styles[tt], `font-weight="bold"`, `class="text-mono-bold"`, -1)
    53  	return strings.Replace(out, `font-style="italic"`, `class="text-mono-italic"`, -1)
    54  }
    55  
    56  // <<< END
    57  

View as plain text