markdown: add markdown rendering for doc/modules.md

Using github.com/yuin/goldmark for Markdown rendering. Rendering only
happens when ./content/static is generated. ./cmd/golangorg does not
need to render anything on request.

Updates golang/go#33637

Change-Id: I68c80b1ed6543f0985848c6e5a58a07da05c972d
Reviewed-on: https://go-review.googlesource.com/c/website/+/218517
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Jay Conrod 2020-02-07 13:17:51 -05:00
Родитель 25de62c822
Коммит 8f9b7478f4
5 изменённых файлов: 65 добавлений и 1 удалений

Просмотреть файл

@ -11,7 +11,10 @@ import (
"fmt"
"go/format"
"io/ioutil"
"strings"
"unicode"
"golang.org/x/website/markdown"
)
var files = []string{
@ -68,21 +71,42 @@ var files = []string{
"style.css",
}
var markdownFiles = []string{
"doc/modules.md",
}
// Generate reads a set of files and returns a file buffer that declares
// a map of string constants containing contents of the input files.
func Generate() ([]byte, error) {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%v\n\n%v\n\npackage static\n\n", license, warning)
fmt.Fprintf(buf, "var Files = map[string]string{\n")
for _, fn := range files {
b, err := ioutil.ReadFile(fn)
if err != nil {
return b, err
return nil, err
}
fmt.Fprintf(buf, "\t%q: ", fn)
appendQuote(buf, b)
fmt.Fprintf(buf, ",\n\n")
}
for _, fn := range markdownFiles {
src, err := ioutil.ReadFile(fn)
if err != nil {
return nil, err
}
gen, err := markdown.Render(src)
if err != nil {
return nil, fmt.Errorf("%s: %v", fn, err)
}
htmlName := strings.TrimSuffix(fn, ".md") + ".html"
fmt.Fprintf(buf, "\t%q: ", htmlName)
appendQuote(buf, gen)
fmt.Fprintf(buf, ",\n\n")
}
fmt.Fprintln(buf, "}")
return format.Source(buf.Bytes())
}

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

1
go.mod
Просмотреть файл

@ -5,6 +5,7 @@ go 1.11
require (
cloud.google.com/go v0.38.0
github.com/gomodule/redigo v2.0.0+incompatible
github.com/yuin/goldmark v1.1.22
go.opencensus.io v0.22.0 // indirect
golang.org/x/build v0.0.0-20191213161705-41fffb13b6ef
golang.org/x/net v0.0.0-20190620200207-3b0461eec859

2
go.sum
Просмотреть файл

@ -44,6 +44,8 @@ github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/yuin/goldmark v1.1.22 h1:0e0f6Zee9SAQ5yOZGNMWaOxqVvcc/9/kUWu/Kl91Jk8=
github.com/yuin/goldmark v1.1.22/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=

35
markdown/markdown.go Normal file
Просмотреть файл

@ -0,0 +1,35 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package markdown provides a wrapper for rendering Markdown. It is intended
// to be used by x/website and x/blog so that we can use the same renderer
// with the same settings.
//
// This package is not intended for general use, and its API is not guaranteed
// to be stable.
package markdown
import (
"bytes"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/renderer/html"
)
// Render converts a limited and opinionated flavor of Markdown (compliant with
// CommonMark 0.29) to HTML for the purposes of golang.org websites. This should
// not be adjusted except for the needs of *.golang.org.
//
// The Markdown source may contain raw HTML and Go templates. Sanitization of
// untrusted content is not performed: the caller is responsible for ensuring
// that only trusted content is provided.
func Render(src []byte) ([]byte, error) {
// html.WithUnsafe allows use of raw HTML, which we need for tables.
md := goldmark.New(goldmark.WithRendererOptions(html.WithUnsafe()))
var buf bytes.Buffer
if err := md.Convert(src, &buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}