-
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.
Add a goldmark parser extension for first class sections
- Loading branch information
Showing
6 changed files
with
281 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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 ( | ||
"bytes" | ||
|
||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" | ||
"github.com/yuin/goldmark" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/extension" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
|
||
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfgen/parse/section" | ||
) | ||
|
||
var TFRegistryExtension goldmark.Extender = tfRegistryExtension{} | ||
|
||
type tfRegistryExtension struct{} | ||
|
||
func (s tfRegistryExtension) Extend(md goldmark.Markdown) { | ||
extension.GFM.Extend(md) | ||
section.Extension(2, 100).Extend(md) | ||
md.Parser().AddOptions( | ||
parser.WithASTTransformers( | ||
util.Prioritized(recognizeHeaderAfterHTML{}, 2000), | ||
)) | ||
|
||
} | ||
|
||
// recognizeHeaderAfterHTML allows us to work around a difference in how TF's registry parses | ||
// markdown vs goldmark's CommonMark parser. | ||
// | ||
// Goldmark correctly (for CommonMark) parses the following as a single HTML Block: | ||
// | ||
// <div> | ||
// content | ||
// </div> | ||
// ## Header | ||
// | ||
// This is a common pattern in GCP, and we need to parse it as a HTML block, then a header | ||
// block. This AST transformation makes the desired change. | ||
type recognizeHeaderAfterHTML struct{} | ||
|
||
func (recognizeHeaderAfterHTML) Transform(node *ast.Document, reader text.Reader, pc parser.Context) { | ||
WalkNode(node, func(node *ast.HTMLBlock) { | ||
if node.Lines().Len() == 0 { | ||
return | ||
} | ||
|
||
last := node.Lines().At(node.Lines().Len() - 1) | ||
if bytes.HasPrefix(last.Value(reader.Source()), []byte("## ")) { | ||
node.Lines().SetSliced(0, node.Lines().Len()-1) | ||
heading := ast.NewHeading(2) | ||
heading.Lines().Append(last) | ||
node.Parent().InsertAfter(node.Parent(), node, heading) | ||
} | ||
}) | ||
} | ||
|
||
func WalkNode[T ast.Node](node ast.Node, f func(T)) { | ||
err := ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) { | ||
n, ok := node.(T) | ||
if ok && entering { | ||
f(n) | ||
} | ||
return ast.WalkContinue, nil | ||
}) | ||
contract.AssertNoErrorf(err, "impossible: ast.Walk never returns an error") | ||
} |
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,82 @@ | ||
// 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 section | ||
|
||
import ( | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" | ||
"github.com/yuin/goldmark" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
var _ goldmark.Extender = section{} | ||
|
||
func Extension(level, priority int) goldmark.Extender { | ||
return section{level, priority} | ||
} | ||
|
||
var Kind = ast.NewNodeKind("Section") | ||
|
||
type section struct{ level, priority int } | ||
|
||
func (s section) Extend(md goldmark.Markdown) { | ||
md.Parser().AddOptions(parser.WithASTTransformers( | ||
util.Prioritized(sectionParser{s.level}, s.priority), | ||
)) | ||
} | ||
|
||
type Section struct{ ast.BaseBlock } | ||
|
||
func (s *Section) Heading() *ast.Heading { | ||
return s.FirstChild().(*ast.Heading) | ||
} | ||
|
||
func (s *Section) Dump(source []byte, level int) { | ||
ast.DumpHelper(s, source, level, nil, nil) | ||
} | ||
|
||
func (s *Section) Kind() ast.NodeKind { return Kind } | ||
|
||
func (s sectionParser) Transform(node *ast.Document, reader text.Reader, pc parser.Context) { | ||
err := ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) { | ||
heading, ok := node.(*ast.Heading) | ||
if !ok || heading.Level != s.level { | ||
return ast.WalkContinue, nil | ||
} | ||
|
||
parent := heading.Parent() | ||
section := &Section{} | ||
c := heading.NextSibling() | ||
section.AppendChild(section, heading) | ||
parent.ReplaceChild(parent, heading, section) | ||
for c != nil { | ||
if child, ok := c.(*ast.Heading); ok && child.Level >= heading.Level { | ||
break | ||
} | ||
child := c | ||
// We are going to add c to section | ||
c = c.NextSibling() | ||
section.AppendChild(section, child) | ||
} | ||
|
||
return ast.WalkContinue, nil | ||
}) | ||
|
||
contract.AssertNoErrorf(err, "The walker cannot error, so the walk cannot error") | ||
} | ||
|
||
type sectionParser struct{ level int } |
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,107 @@ | ||
// 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 section_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/hexops/autogold/v2" | ||
"github.com/stretchr/testify/require" | ||
markdown "github.com/teekennedy/goldmark-markdown" | ||
"github.com/yuin/goldmark" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
|
||
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfgen/parse/section" | ||
) | ||
|
||
type walkTransformer func(node ast.Node, entering bool) (ast.WalkStatus, error) | ||
|
||
func (w walkTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) { | ||
err := ast.Walk(node, (func(node ast.Node, entering bool) (ast.WalkStatus, error))(w)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func TestSection(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
input string | ||
walk func(src []byte, node ast.Node, entering bool) (ast.WalkStatus, error) | ||
expected autogold.Value | ||
}{ | ||
{ | ||
input: ` | ||
Hi | ||
## 1 | ||
content *foo* | ||
content | ||
## 2 | ||
content (again) | ||
`, | ||
walk: func(src []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { | ||
s, ok := node.(*section.Section) | ||
if !ok || !entering { | ||
return ast.WalkContinue, nil | ||
} | ||
s.Dump(src, 0) | ||
if string(s.FirstChild().(*ast.Heading).Text(src)) == "1" { | ||
s.Parent().RemoveChild(s.Parent(), s) | ||
} | ||
return ast.WalkContinue, nil | ||
}, | ||
expected: autogold.Expect(`Hi | ||
## 2 | ||
content (again) | ||
`), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run("", func(t *testing.T) { | ||
t.Parallel() | ||
src := []byte(tt.input) | ||
walk := func(node ast.Node, entering bool) (ast.WalkStatus, error) { | ||
return tt.walk(src, node, entering) | ||
} | ||
var b bytes.Buffer | ||
gm := goldmark.New( | ||
goldmark.WithExtensions(section.Extension(2, 100)), | ||
goldmark.WithParserOptions( | ||
parser.WithASTTransformers( | ||
util.Prioritized(walkTransformer(walk), 2000), | ||
), | ||
), | ||
goldmark.WithRenderer(markdown.NewRenderer()), | ||
) | ||
require.NoError(t, gm.Convert(src, &b)) | ||
tt.expected.Equal(t, b.String()) | ||
}) | ||
} | ||
} |