Skip to content

Commit

Permalink
adds env var RUNME_AST_METADATA
Browse files Browse the repository at this point in the history
  • Loading branch information
pastuxso committed Oct 20, 2023
1 parent eaedb6e commit 46ec3f3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion examples/grpc-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ func run() error {
client := parserv1.NewParserServiceClient(conn)

resp, err := client.Deserialize(context.Background(), &parserv1.DeserializeRequest{
Source: []byte(""),
Source: []byte("# Hello"),
})
if err != nil {
return err
}

_, _ = fmt.Println(resp.Notebook)
_, _ = fmt.Println(resp.Notebook.Cells[0].Metadata)

return nil
}
Expand Down
19 changes: 13 additions & 6 deletions internal/document/editor/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"log"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -150,13 +151,19 @@ func toCellsRec(
astNode := block.Unwrap()

metadata := make(map[string]string)
astMetadata := DumpToMap(astNode, source, astNode.Kind().String())
jsonAstMetaData, err := json.Marshal(astMetadata)
if err != nil {
log.Fatalf("Error converting to JSON: %s", err)
}
_, includeAstMetadata := os.LookupEnv("RUNME_AST_METADATA")

if includeAstMetadata {
astMetadata := DumpToMap(astNode, source, astNode.Kind().String())
jsonAstMetaData, err := json.Marshal(astMetadata)
if err != nil {
log.Fatalf("Error converting to JSON: %s", err)
}

metadata["runme.dev/ast"] = string(jsonAstMetaData)
env := os.Environ()
fmt.Println(env)
metadata["runme.dev/ast"] = string(jsonAstMetaData)
}

isListItem := node.Item() != nil && node.Item().Unwrap().Kind() == ast.KindListItem
if childIdx == 0 && isListItem {
Expand Down
2 changes: 2 additions & 0 deletions internal/document/editor/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package editor
import (
"bytes"
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -105,6 +106,7 @@ Paragraph 1 with a link [Link1](https://example.com 'Link Title 1') and a second
#### Heading Level 4
##### Heading Level 5
`)
os.Setenv("RUNME_AST_METADATA", "true")
notebook, err := Deserialize(data)
require.NoError(t, err)
require.NotEmpty(t, notebook.Metadata)
Expand Down
3 changes: 3 additions & 0 deletions internal/document/editor/editorservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package editorservice
import (
"context"
"net"
"os"
"testing"

"github.com/stateful/runme/internal/document/editor"
Expand Down Expand Up @@ -33,6 +34,8 @@ func Test_parserServiceServer(t *testing.T) {
client := parserv1.NewParserServiceClient(conn)

t.Run("Basic", func(t *testing.T) {
os.Setenv("RUNME_AST_METADATA", "true")

resp, err := client.Deserialize(
context.Background(),
&parserv1.DeserializeRequest{
Expand Down

0 comments on commit 46ec3f3

Please sign in to comment.