Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve 01-hello to be frendlier #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const parseTutorial = (directory: string): TutorialItem => {
const codeFilePath = path.join(directory, filePath);
let codeContent = fs.readFileSync(codeFilePath, 'utf-8');


// Check if there are any specific code segments that need embedding
if (/#L\d+-L\d+/.test(codeRef)) {
const [startLine, endLine] =
Expand All @@ -103,7 +103,7 @@ const parseTutorial = (directory: string): TutorialItem => {
}

codeContent = lines.slice(startLine - 1, endLine).join('\n')
}
}

let files = [];

Expand All @@ -115,18 +115,21 @@ const parseTutorial = (directory: string): TutorialItem => {
files.push({ path: codeFilePath, content: codeContent })
})

const filesEncoded = encodeURIComponent(JSON.stringify(files))
const filesEncoded = encodeURIComponent(JSON.stringify(files))

const embeddedCodeBlock = `<Playground open="${codeFilePath}" files="${filesEncoded}">`;

mdContent = mdContent.replace(codeRef, embeddedCodeBlock);
}

// Make sure to escape all other backticks
mdContent = mdContent.replace(/([^\\])`/g, '$1\\`');
mdContent = mdContent.
// Escape backslashes
replace(/\\/g, '\\\\').
// Escape all other backticks
replace(/([^\\])`/g, '$1\\`').
// Drop the metadata section
replace(/---[\s\S]*?---/, '');

// Make sure to drop the metadata section
mdContent = mdContent.replace(/---[\s\S]*?---/, '');

const tsFilePath = path.join(directory, 'index.ts');
let output: string = `const markdownContent: string = \n\`${mdContent}\`;\n`;
Expand Down
21 changes: 18 additions & 3 deletions src/tutorials/gno.land/gbe/01-hello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ title: Hello World
section: Getting Started
---

This is the simplest application that you can create using Gno. `Render(string) string` is a special method that will be
used by `gnoweb` to render markdown output.
Whether you're a seasoned Go developer, or you're just dipping your toes into
Gno, let's start off on the right foot with a ritual
[Hello World](https://en.wikipedia.org/wiki/%22Hello,_World!%22_program) program!

Being designed to run smart contracts, there is no such thing as a "main
program" or "main function" as you may be used to in other programming
languages. However, the [gno.land](https://gno.land) blockchain allows having
programs with a special function, `Render(string) string`.

This function is called when loading programs from the test nodes' website. The
website will also parse it as markdown and render it, so it's a good idea to add
some **\*\*spice!\*\*** to it.

```go file=./hello.gno
```

We can test the `Render` method:
On this website, to show how each function is working we'll be using tests;
which work similarly to how they do in Go.

You can define tests in function ending with the `_test.gno` suffix. These can
be executed by clicking on the `Test` button, available from the menu hidden in
the bottom right circle.

```go file=./hello_test.gno depends_on_file=./hello.gno
```
4 changes: 2 additions & 2 deletions src/tutorials/gno.land/gbe/01-hello/hello.gno
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package hello

func Render(path string) string {
return "Hello World!"
}
return "Hello World! And hello, **bold!**"
}
4 changes: 2 additions & 2 deletions src/tutorials/gno.land/gbe/01-hello/hello_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "testing"

func TestHello(t *testing.T) {
got := Render("")
expected := "Hello World!"
expected := "Hello World! And hello, **bold!**"
if got != expected {
t.Fatalf("expected %q, got %q.", expected, got)
}
}
}
25 changes: 20 additions & 5 deletions src/tutorials/gno.land/gbe/01-hello/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
const markdownContent: string = `

This is the simplest application that you can create using Gno. \`Render(string) string\` is a special method that will be
used by \`gnoweb\` to render markdown output.
Whether you're a seasoned Go developer, or you're just dipping your toes into
Gno, let's start off on the right foot with a ritual
[Hello World](https://en.wikipedia.org/wiki/%22Hello,_World!%22_program) program!

<Playground open="src/tutorials/gno.land/gbe/01-hello/hello.gno" files="%5B%7B%22path%22%3A%22src%2Ftutorials%2Fgno.land%2Fgbe%2F01-hello%2Fhello.gno%22%2C%22content%22%3A%22package%20hello%5Cn%5Cnfunc%20Render(path%20string)%20string%20%7B%5Cn%5Ctreturn%20%5C%22Hello%20World!%5C%22%5Cn%7D%22%7D%5D">
Being designed to run smart contracts, there is no such thing as a "main
program" or "main function" as you may be used to in other programming
languages. However, the [gno.land](https://gno.land) blockchain allows having
programs with a special function, \`Render(string) string\`.

We can test the \`Render\` method:
This function is called when loading programs from the test nodes' website. The
website will also parse it as markdown and render it, so it's a good idea to add
some **\\*\\*spice!\\*\\*** to it.

<Playground open="src/tutorials/gno.land/gbe/01-hello/hello_test.gno" files="%5B%7B%22path%22%3A%22src%2Ftutorials%2Fgno.land%2Fgbe%2F01-hello%2Fhello_test.gno%22%2C%22content%22%3A%22package%20hello%5Cn%5Cnimport%20%5C%22testing%5C%22%5Cn%5Cnfunc%20TestHello(t%20*testing.T)%20%7B%5Cn%5Ctgot%20%3A%3D%20Render(%5C%22%5C%22)%5Cn%5Ctexpected%20%3A%3D%20%5C%22Hello%20World!%5C%22%5Cn%5Ctif%20got%20!%3D%20expected%20%7B%5Cn%5Ct%5Ctt.Fatalf(%5C%22expected%20%25q%2C%20got%20%25q.%5C%22%2C%20expected%2C%20got)%5Cn%5Ct%7D%5Cn%7D%22%7D%2C%7B%22path%22%3A%22src%2Ftutorials%2Fgno.land%2Fgbe%2F01-hello%2Fhello.gno%22%2C%22content%22%3A%22package%20hello%5Cn%5Cnfunc%20Render(path%20string)%20string%20%7B%5Cn%5Ctreturn%20%5C%22Hello%20World!%5C%22%5Cn%7D%22%7D%5D">
<Playground open="src/tutorials/gno.land/gbe/01-hello/hello.gno" files="%5B%7B%22path%22%3A%22src%2Ftutorials%2Fgno.land%2Fgbe%2F01-hello%2Fhello.gno%22%2C%22content%22%3A%22package%20hello%5Cn%5Cnfunc%20Render(path%20string)%20string%20%7B%5Cn%5Ctreturn%20%5C%22Hello%20World!%20And%20hello%2C%20**bold!**%5C%22%5Cn%7D%5Cn%22%7D%5D">

On this website, to show how each function is working we'll be using tests;
which work similarly to how they do in Go.

You can define tests in function ending with the \`_test.gno\` suffix. These can
be executed by clicking on the \`Test\` button, available from the menu hidden in
the bottom right circle.

<Playground open="src/tutorials/gno.land/gbe/01-hello/hello_test.gno" files="%5B%7B%22path%22%3A%22src%2Ftutorials%2Fgno.land%2Fgbe%2F01-hello%2Fhello_test.gno%22%2C%22content%22%3A%22package%20hello%5Cn%5Cnimport%20%5C%22testing%5C%22%5Cn%5Cnfunc%20TestHello(t%20*testing.T)%20%7B%5Cn%5Ctgot%20%3A%3D%20Render(%5C%22%5C%22)%5Cn%5Ctexpected%20%3A%3D%20%5C%22Hello%20World!%20And%20hello%2C%20**bold!**%5C%22%5Cn%5Ctif%20got%20!%3D%20expected%20%7B%5Cn%5Ct%5Ctt.Fatalf(%5C%22expected%20%25q%2C%20got%20%25q.%5C%22%2C%20expected%2C%20got)%5Cn%5Ct%7D%5Cn%7D%5Cn%22%7D%2C%7B%22path%22%3A%22src%2Ftutorials%2Fgno.land%2Fgbe%2F01-hello%2Fhello.gno%22%2C%22content%22%3A%22package%20hello%5Cn%5Cnfunc%20Render(path%20string)%20string%20%7B%5Cn%5Ctreturn%20%5C%22Hello%20World!%20And%20hello%2C%20**bold!**%5C%22%5Cn%7D%5Cn%22%7D%5D">
`;

const title: string = "Hello World";
Expand Down