Skip to content

Commit

Permalink
Skip private attributes incells serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Dec 12, 2022
1 parent 3de40c3 commit eef463c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
11 changes: 8 additions & 3 deletions internal/document/edit/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"strconv"
"strings"

"github.com/stateful/runme/internal/document"
"github.com/yuin/goldmark/ast"
Expand Down Expand Up @@ -155,12 +156,16 @@ func serializeFencedCodeAttributes(w io.Writer, cell *Cell) {

_, _ = w.Write([]byte{' ', '{', ' '})

// Sort attributes by key, however, keep the element
// with the key "name" in front.
// Filter out private keys, i.e. starting with "_" or "runme.dev/".
keys := make([]string, 0, len(cell.Metadata))
for k := range cell.Metadata {
if strings.HasPrefix(k, "_") || strings.HasPrefix(k, "runme.dev/") {
continue
}
keys = append(keys, k)
}
// Sort attributes by key, however, keep the element
// with the key "name" in front.
slices.SortFunc(keys, func(a, b string) bool {
if a == "name" {
return true
Expand All @@ -176,7 +181,7 @@ func serializeFencedCodeAttributes(w io.Writer, cell *Cell) {
v := cell.Metadata[k]
_, _ = w.Write([]byte(fmt.Sprintf("%s=%v", k, v)))
i++
if i < len(cell.Metadata) {
if i < len(keys) {
_, _ = w.Write([]byte{' '})
}
}
Expand Down
32 changes: 23 additions & 9 deletions internal/document/edit/cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,40 @@ pre-commit install
)
}

func Test_serializeCells_unknownLang(t *testing.T) {
data := []byte(`## Non-Supported Languages
` + "```py { readonly=true }" + `
def hello():
print("Hello World")
` + "```" + `
`)
func Test_serializeCells_attributes(t *testing.T) {
data := []byte("```sh { name=echo first= second=2 }\necho 1\n```\n")
doc := document.New(data, cmark.Render)
node, _, err := doc.Parse()
require.NoError(t, err)
cells := toCells(node, data)
assert.Equal(t, string(data), string(serializeCells(cells)))
}

func Test_serializeCells_attributes(t *testing.T) {
func Test_serializeCells_privateFields(t *testing.T) {
data := []byte("```sh { name=echo first= second=2 }\necho 1\n```\n")
doc := document.New(data, cmark.Render)
node, _, err := doc.Parse()
require.NoError(t, err)

cells := toCells(node, data)
// Add private fields whcih will be filtered out durign serialization.
cells[0].Metadata["_private"] = "private"
cells[0].Metadata["runme.dev/internal"] = "internal"

assert.Equal(t, string(data), string(serializeCells(cells)))
}

func Test_serializeCells_unknownLang(t *testing.T) {
data := []byte(`## Non-Supported Languages
` + "```py { readonly=true }" + `
def hello():
print("Hello World")
` + "```" + `
`)
doc := document.New(data, cmark.Render)
node, _, err := doc.Parse()
require.NoError(t, err)
cells := toCells(node, data)
assert.Equal(t, string(data), string(serializeCells(cells)))
}

0 comments on commit eef463c

Please sign in to comment.