| 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 goldmark |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"strconv" |
| 19 |
"unicode" |
| 20 |
"unicode/utf8" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/markup/blackfriday" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config" |
| 25 |
|
| 26 |
"github.com/gohugoio/hugo/common/text" |
| 27 |
|
| 28 |
"github.com/yuin/goldmark/ast" |
| 29 |
east "github.com/yuin/goldmark/extension/ast" |
| 30 |
"github.com/yuin/goldmark/parser" |
| 31 |
"github.com/yuin/goldmark/util" |
| 32 |
|
| 33 |
bp "github.com/gohugoio/hugo/bufferpool" |
| 34 |
) |
| 35 |
|
| 36 |
func sanitizeAnchorNameString(s string, idType string) string { |
| 37 |
return string(sanitizeAnchorName([]byte(s), idType)) |
| 38 |
} |
| 39 |
|
| 40 |
func sanitizeAnchorName(b []byte, idType string) []byte { |
| 41 |
return sanitizeAnchorNameWithHook(b, idType, nil) |
| 42 |
} |
| 43 |
|
| 44 |
func sanitizeAnchorNameWithHook(b []byte, idType string, hook func(buf *bytes.Buffer)) []byte { |
| 45 |
buf := bp.GetBuffer() |
| 46 |
|
| 47 |
if idType == goldmark_config.AutoIDTypeBlackfriday { |
| 48 |
// TODO(bep) make it more efficient. |
| 49 |
buf.WriteString(blackfriday.SanitizedAnchorName(string(b))) |
| 50 |
} else { |
| 51 |
asciiOnly := idType == goldmark_config.AutoIDTypeGitHubAscii |
| 52 |
|
| 53 |
if asciiOnly { |
| 54 |
// Normalize it to preserve accents if possible. |
| 55 |
b = text.RemoveAccents(b) |
| 56 |
} |
| 57 |
|
| 58 |
b = bytes.TrimSpace(b) |
| 59 |
|
| 60 |
for len(b) > 0 { |
| 61 |
r, size := utf8.DecodeRune(b) |
| 62 |
switch { |
| 63 |
case asciiOnly && size != 1: |
| 64 |
case r == '-' || r == ' ': |
| 65 |
buf.WriteRune('-') |
| 66 |
case isAlphaNumeric(r): |
| 67 |
buf.WriteRune(unicode.ToLower(r)) |
| 68 |
default: |
| 69 |
} |
| 70 |
|
| 71 |
b = b[size:] |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if hook != nil { |
| 76 |
hook(buf) |
| 77 |
} |
| 78 |
|
| 79 |
result := make([]byte, buf.Len()) |
| 80 |
copy(result, buf.Bytes()) |
| 81 |
|
| 82 |
bp.PutBuffer(buf) |
| 83 |
|
| 84 |
return result |
| 85 |
} |
| 86 |
|
| 87 |
func isAlphaNumeric(r rune) bool { |
| 88 |
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r) |
| 89 |
} |
| 90 |
|
| 91 |
var _ parser.IDs = (*idFactory)(nil) |
| 92 |
|
| 93 |
type idFactory struct { |
| 94 |
idType string |
| 95 |
vals map[string]struct{} |
| 96 |
duplicates []string |
| 97 |
} |
| 98 |
|
| 99 |
func newIDFactory(idType string) *idFactory { |
| 100 |
return &idFactory{ |
| 101 |
vals: make(map[string]struct{}), |
| 102 |
idType: idType, |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
type stringValuesProvider interface { |
| 107 |
StringValues() []string |
| 108 |
} |
| 109 |
|
| 110 |
var _ stringValuesProvider = (*idFactory)(nil) |
| 111 |
|
| 112 |
func (ids *idFactory) StringValues() []string { |
| 113 |
values := make([]string, 0, len(ids.vals)) |
| 114 |
for k := range ids.vals { |
| 115 |
values = append(values, k) |
| 116 |
} |
| 117 |
values = append(values, ids.duplicates...) |
| 118 |
return values |
| 119 |
} |
| 120 |
|
| 121 |
func (ids *idFactory) Generate(value []byte, kind ast.NodeKind) []byte { |
| 122 |
return sanitizeAnchorNameWithHook(value, ids.idType, func(buf *bytes.Buffer) { |
| 123 |
if buf.Len() == 0 { |
| 124 |
if kind == ast.KindHeading { |
| 125 |
buf.WriteString("heading") |
| 126 |
} else if kind == east.KindDefinitionTerm { |
| 127 |
buf.WriteString("term") |
| 128 |
} else { |
| 129 |
buf.WriteString("id") |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if _, found := ids.vals[util.BytesToReadOnlyString(buf.Bytes())]; found { |
| 134 |
// Append a hyphen and a number, starting with 1. |
| 135 |
buf.WriteRune('-') |
| 136 |
pos := buf.Len() |
| 137 |
for i := 1; ; i++ { |
| 138 |
buf.WriteString(strconv.Itoa(i)) |
| 139 |
if _, found := ids.vals[util.BytesToReadOnlyString(buf.Bytes())]; !found { |
| 140 |
break |
| 141 |
} |
| 142 |
buf.Truncate(pos) |
| 143 |
} |
| 144 |
} |
| 145 |
ids.put(buf.String()) |
| 146 |
}) |
| 147 |
} |
| 148 |
|
| 149 |
func (ids *idFactory) put(s string) { |
| 150 |
if _, found := ids.vals[s]; found { |
| 151 |
ids.duplicates = append(ids.duplicates, s) |
| 152 |
} else { |
| 153 |
ids.vals[s] = struct{}{} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func (ids *idFactory) Put(value []byte) { |
| 158 |
ids.put(string(value)) |
| 159 |
} |