-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use github.com/yuin/goldmark-meta to parse the meta section
Because later parse steps look for the `---` that delineates a meta section, we need to not only parse but also render back the meta section. We should be able to remove this when we start delineating sections by node and node kind instead of by re-"parsing" the document.
- Loading branch information
Showing
12 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2016-2024, Pulumi Corporation. | ||
// | ||
// 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 parse | ||
|
||
import ( | ||
"github.com/yuin/goldmark" | ||
meta "github.com/yuin/goldmark-meta" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/renderer" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var MetaKind = ast.NewNodeKind("Meta") | ||
|
||
type Meta struct { | ||
ast.BaseBlock | ||
Yaml yaml.MapSlice | ||
} | ||
|
||
func (m *Meta) Dump(source []byte, level int) { | ||
ast.DumpHelper(m, source, level, nil, nil) | ||
} | ||
|
||
func (m *Meta) Kind() ast.NodeKind { return MetaKind } | ||
|
||
var _ goldmark.Extender = metaExtension{} | ||
|
||
type metaExtension struct{} | ||
|
||
func (m metaExtension) Extend(md goldmark.Markdown) { | ||
md.Parser().AddOptions(parser.WithASTTransformers( | ||
util.Prioritized(m, 1), | ||
)) | ||
md.Renderer().AddOptions(renderer.WithNodeRenderers( | ||
util.Prioritized(m, 1), | ||
)) | ||
} | ||
|
||
func (metaExtension) Transform(node *ast.Document, reader text.Reader, pc parser.Context) { | ||
items := meta.GetItems(pc) | ||
if items == nil { | ||
return | ||
} | ||
node.InsertBefore(node, node.FirstChild(), &Meta{Yaml: items}) | ||
} | ||
|
||
func (m metaExtension) RegisterFuncs(r renderer.NodeRendererFuncRegisterer) { | ||
f := func(b util.BufWriter, _ []byte, n ast.Node, entering bool) (ast.WalkStatus, error) { | ||
if entering { | ||
return ast.WalkContinue, nil | ||
} | ||
b.WriteString("---\n") | ||
y, err := yaml.Marshal(n.(*Meta).Yaml) | ||
if err != nil { | ||
return ast.WalkContinue, err | ||
} | ||
b.Write(y) | ||
b.WriteByte('\n') | ||
b.WriteString("---\n") | ||
return ast.WalkContinue, nil | ||
} | ||
r.Register(MetaKind, f) | ||
} |