mirror of https://github.com/jlelse/GoBlog
Simplify emojify
This commit is contained in:
parent
caad3415c0
commit
5bb295b4ed
75
emoji.go
75
emoji.go
|
@ -1,75 +0,0 @@
|
|||
// This is taken from Hugo
|
||||
//
|
||||
// Copyright 2016 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
|
||||
"github.com/kyokomi/emoji"
|
||||
)
|
||||
|
||||
var (
|
||||
emojiInit sync.Once
|
||||
|
||||
emojis = make(map[string][]byte)
|
||||
|
||||
emojiDelim = []byte(":")
|
||||
emojiWordDelim = []byte(" ")
|
||||
emojiMaxSize int
|
||||
)
|
||||
|
||||
func emojify(source []byte) []byte {
|
||||
emojiInit.Do(initEmoji)
|
||||
start := 0
|
||||
k := bytes.Index(source[start:], emojiDelim)
|
||||
for k != -1 {
|
||||
j := start + k
|
||||
upper := j + emojiMaxSize
|
||||
if upper > len(source) {
|
||||
upper = len(source)
|
||||
}
|
||||
endEmoji := bytes.Index(source[j+1:upper], emojiDelim)
|
||||
nextWordDelim := bytes.Index(source[j:upper], emojiWordDelim)
|
||||
if endEmoji < 0 {
|
||||
start++
|
||||
} else if endEmoji == 0 || (nextWordDelim != -1 && nextWordDelim < endEmoji) {
|
||||
start += endEmoji + 1
|
||||
} else {
|
||||
endKey := endEmoji + j + 2
|
||||
emojiKey := source[j:endKey]
|
||||
if e, ok := emojis[string(emojiKey)]; ok {
|
||||
source = append(source[:j], append(e, source[endKey:]...)...)
|
||||
}
|
||||
start += endEmoji
|
||||
}
|
||||
if start >= len(source) {
|
||||
break
|
||||
}
|
||||
k = bytes.Index(source[start:], emojiDelim)
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
func initEmoji() {
|
||||
emojiMap := emoji.CodeMap()
|
||||
for k, v := range emojiMap {
|
||||
emojis[k] = []byte(v)
|
||||
if len(k) > emojiMaxSize {
|
||||
emojiMaxSize = len(k)
|
||||
}
|
||||
}
|
||||
}
|
2
main.go
2
main.go
|
@ -18,7 +18,7 @@ func main() {
|
|||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
initEmoji()
|
||||
initMarkdown()
|
||||
initRendering()
|
||||
initMinify()
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
_ "bytes"
|
||||
"github.com/kyokomi/emoji"
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
var markdown goldmark.Markdown
|
||||
|
||||
func init() {
|
||||
func initMarkdown() {
|
||||
markdown = goldmark.New(
|
||||
goldmark.WithRendererOptions(
|
||||
html.WithUnsafe(),
|
||||
|
@ -28,9 +28,8 @@ func init() {
|
|||
}
|
||||
|
||||
func renderMarkdown(source string) (content []byte, err error) {
|
||||
context := parser.NewContext()
|
||||
var buffer bytes.Buffer
|
||||
err = markdown.Convert([]byte(source), &buffer, parser.WithContext(context))
|
||||
content = emojify(buffer.Bytes())
|
||||
err = markdown.Convert([]byte(emoji.Sprint(source)), &buffer)
|
||||
content = buffer.Bytes()
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue