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

feat(examples): Implement markdown package #2912

Merged
merged 35 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6d57efa
first commit
sunspirit99 Oct 5, 2024
6c89645
fixup
sunspirit99 Oct 5, 2024
451b785
refactor
sunspirit99 Oct 5, 2024
61a04b9
Merge branch 'master' into implement-markdown-library
sunspirit99 Oct 5, 2024
3644ac0
gno mod tidy
sunspirit99 Oct 5, 2024
7c7fa50
fixup
sunspirit99 Oct 5, 2024
6ac3db4
make fmt
sunspirit99 Oct 5, 2024
b469e5e
Merge branch 'master' into implement-markdown-library
sunspirit99 Oct 7, 2024
0aaf911
rename to md & refactor TodoList
sunspirit99 Oct 8, 2024
8674cf0
define Paragraph function
sunspirit99 Oct 8, 2024
8a4103e
update Paragraph()
sunspirit99 Oct 8, 2024
3057bca
seperate table lib from md
sunspirit99 Oct 8, 2024
4064fd1
update Paragraph()
sunspirit99 Oct 8, 2024
cec705c
fix ci
sunspirit99 Oct 8, 2024
b2a9bb0
fix module name
sunspirit99 Oct 8, 2024
2eef2a9
update
sunspirit99 Oct 8, 2024
e26eb4a
make tidy
sunspirit99 Oct 8, 2024
3be4662
Merge branch 'master' into implement-markdown-library
sunspirit99 Oct 8, 2024
c472d9b
fix the unit tests
sunspirit99 Oct 8, 2024
6e66be1
update new func
sunspirit99 Oct 11, 2024
3a06294
fixup
sunspirit99 Oct 11, 2024
bc217a6
Merge branch 'master' into implement-markdown-library
sunspirit99 Oct 11, 2024
a3129fa
improve render md
sunspirit99 Oct 23, 2024
c91d79b
make fmt
sunspirit99 Oct 23, 2024
30a75e0
rename method
sunspirit99 Nov 9, 2024
8cf816a
Merge branch 'master' into implement-markdown-library
sunspirit99 Nov 9, 2024
752ebf3
move code to my namespace
sunspirit99 Nov 9, 2024
ee3ee24
make fmt
sunspirit99 Nov 9, 2024
b05042a
optional: enable making md document with buider
sunspirit99 Nov 9, 2024
f04d0e6
make tidy
sunspirit99 Nov 9, 2024
0ece7a7
make tidy
sunspirit99 Nov 9, 2024
bbbf75f
make code more readable
sunspirit99 Nov 10, 2024
be3a999
use variadic params for Add function
sunspirit99 Nov 10, 2024
845547d
Merge branch 'master' into implement-markdown-library
leohhhn Feb 6, 2025
e8d5031
fix mod tidy
leohhhn Feb 6, 2025
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
7 changes: 7 additions & 0 deletions examples/gno.land/p/demo/md/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/p/demo/md

require (
gno.land/p/demo/table v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
158 changes: 158 additions & 0 deletions examples/gno.land/p/demo/md/md.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package md

import (
"strings"

"gno.land/p/demo/ufmt"
)

// Bold returns bold text for markdown
func Bold(text string) string {
return ufmt.Sprintf("**%s**", text)
}

// Italic returns italicized text for markdown
func Italic(text string) string {
return ufmt.Sprintf("*%s*", text)
}

// Strikethrough returns strikethrough text for markdown
func Strikethrough(text string) string {
return ufmt.Sprintf("~~%s~~", text)
}

// H1 returns a level 1 header for markdown
func H1(text string) string {
return ufmt.Sprintf("# %s", text)
}

// H2 returns a level 2 header for markdown
func H2(text string) string {
return ufmt.Sprintf("## %s", text)
}

// H3 returns a level 3 header for markdown
func H3(text string) string {
return ufmt.Sprintf("### %s", text)
}

// H4 returns a level 4 header for markdown
func H4(text string) string {
return ufmt.Sprintf("#### %s", text)
}

// H5 returns a level 5 header for markdown
func H5(text string) string {
return ufmt.Sprintf("##### %s", text)
}

// H6 returns a level 6 header for markdown
func H6(text string) string {
return ufmt.Sprintf("###### %s", text)
}

// BulletList returns an bullet list for markdown
func BulletList(items []string) string {
var sb strings.Builder
for _, item := range items {
sb.WriteString(ufmt.Sprintf("- %s\n", item))
}
return sb.String()
}

// OrderedList returns an ordered list for markdown
func OrderedList(items []string) string {
var sb strings.Builder
for i, item := range items {
sb.WriteString(ufmt.Sprintf("%d. %s\n", i+1, item))
}
return sb.String()
}

// TodoList returns a list of todo items with checkboxes for markdown
func TodoList(items []string, done []bool) string {
var sb strings.Builder

for i, item := range items {
checkbox := " "
if done[i] {
checkbox = "x"
}
sb.WriteString(ufmt.Sprintf("- [%s] %s\n", checkbox, item))
}
return sb.String()
}

// Blockquote returns a blockquote for markdown
func Blockquote(text string) string {
lines := strings.Split(text, "\n")
var sb strings.Builder
for _, line := range lines {
sb.WriteString(ufmt.Sprintf("> %s\n", line))
}

return sb.String()
}

// InlineCode returns inline code for markdown
func InlineCode(code string) string {
return ufmt.Sprintf("`%s`", code)
}

// CodeBlock creates a markdown code block
func CodeBlock(content string) string {
return ufmt.Sprintf("```\n%s\n```", content)
}

// LanguageCodeBlock creates a markdown code block with language-specific syntax highlighting
func LanguageCodeBlock(language, content string) string {
return ufmt.Sprintf("```%s\n%s\n```", language, content)
}

// LineBreak returns the specified number of line breaks for markdown
func LineBreak(count uint) string {
if count > 0 {
return strings.Repeat("\n", int(count)+1)
}
return ""
}

// HorizontalRule returns a horizontal rule for markdown
func HorizontalRule() string {
return "---\n"
}

// Link returns a hyperlink for markdown
func Link(text, url string) string {
return ufmt.Sprintf("[%s](%s)", text, url)
}

// Image returns an image for markdown
func Image(altText, url string) string {
return ufmt.Sprintf("![%s](%s)", altText, url)
}

// Footnote returns a footnote for markdown
func Footnote(reference, text string) string {
return ufmt.Sprintf("[%s]: %s", reference, text)
}

// Paragraph wraps the given text in a Markdown paragraph
func Paragraph(content string) string {
return ufmt.Sprintf("%s\n", content)
}

// MdTable is an interface for table types that can be converted to Markdown format
type MdTable interface {
ToMarkdown() string
}

// Table takes any MdTable implementation and returns its markdown representation
func Table(table MdTable) string {
return table.ToMarkdown()
}

// EscapeMarkdown escapes special markdown characters in a string
func EscapeMarkdown(text string) string {
return ufmt.Sprintf("``%s``", text)
}
142 changes: 142 additions & 0 deletions examples/gno.land/p/demo/md/md_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package md

import (
"testing"

"gno.land/p/demo/table"
"gno.land/p/demo/uassert"
)

func Test_Bold(t *testing.T) {
uassert.Equal(t, Bold("Hello"), "**Hello**")
}

func Test_Italic(t *testing.T) {
uassert.Equal(t, Italic("Hello"), "*Hello*")
}

func Test_Strikethrough(t *testing.T) {
uassert.Equal(t, Strikethrough("Hello"), "~~Hello~~")
}

func Test_H1(t *testing.T) {
uassert.Equal(t, H1("Header 1"), "# Header 1")
}

func Test_H2(t *testing.T) {
uassert.Equal(t, H2("Header 2"), "## Header 2")
}

func Test_H3(t *testing.T) {
uassert.Equal(t, H3("Header 3"), "### Header 3")
}

func Test_H4(t *testing.T) {
uassert.Equal(t, H4("Header 4"), "#### Header 4")
}

func Test_H5(t *testing.T) {
uassert.Equal(t, H5("Header 5"), "##### Header 5")
}

func Test_H6(t *testing.T) {
uassert.Equal(t, H6("Header 6"), "###### Header 6")
}

func Test_BulletList(t *testing.T) {
items := []string{"Item 1", "Item 2", "Item 3"}
result := BulletList(items)
expected := "- Item 1\n- Item 2\n- Item 3\n"
uassert.Equal(t, result, expected)
}

func Test_OrderedList(t *testing.T) {
items := []string{"Item 1", "Item 2", "Item 3"}
result := OrderedList(items)
expected := "1. Item 1\n2. Item 2\n3. Item 3\n"
uassert.Equal(t, result, expected)
}

func Test_TodoList(t *testing.T) {
items := []string{"Task 1", "Task 2"}
done := []bool{true, false}
result := TodoList(items, done)
expected := "- [x] Task 1\n- [ ] Task 2\n"
uassert.Equal(t, result, expected)
}

func Test_Blockquote(t *testing.T) {
text := "This is a blockquote.\nIt has multiple lines."
result := Blockquote(text)
expected := "> This is a blockquote.\n> It has multiple lines.\n"
uassert.Equal(t, result, expected)
}

func Test_InlineCode(t *testing.T) {
result := InlineCode("code")
uassert.Equal(t, result, "`code`")
}

func Test_LanguageCodeBlock(t *testing.T) {
result := LanguageCodeBlock("python", "print('Hello')")
expected := "```python\nprint('Hello')\n```"
uassert.Equal(t, result, expected)
}

func Test_CodeBlock(t *testing.T) {
result := CodeBlock("print('Hello')")
expected := "```\nprint('Hello')\n```"
uassert.Equal(t, result, expected)
}

func Test_LineBreak(t *testing.T) {
result := LineBreak(2)
expected := "\n\n\n"
uassert.Equal(t, result, expected)

result = LineBreak(0)
expected = ""
uassert.Equal(t, result, expected)
}

func Test_HorizontalRule(t *testing.T) {
result := HorizontalRule()
uassert.Equal(t, result, "---\n")
}

func Test_Link(t *testing.T) {
result := Link("Google", "http://google.com")
uassert.Equal(t, result, "[Google](http://google.com)")
}

func Test_Image(t *testing.T) {
result := Image("Alt text", "http://image.url")
uassert.Equal(t, result, "![Alt text](http://image.url)")
}

func Test_Footnote(t *testing.T) {
result := Footnote("1", "This is a footnote.")
uassert.Equal(t, result, "[1]: This is a footnote.")
}

func Test_Paragraph(t *testing.T) {
result := Paragraph("This is a paragraph.")
uassert.Equal(t, result, "This is a paragraph.\n")
}

func Test_Table(t *testing.T) {
tb, err := table.New([]string{"Header1", "Header2"}, [][]string{
{"Row1Col1", "Row1Col2"},
{"Row2Col1", "Row2Col2"},
})
uassert.NoError(t, err)

result := Table(tb)
expected := "| Header1 | Header2 |\n| ---|---|\n| Row1Col1 | Row1Col2 |\n| Row2Col1 | Row2Col2 |\n"
uassert.Equal(t, result, expected)
}

func Test_EscapeMarkdown(t *testing.T) {
result := EscapeMarkdown("- This is `code`")
uassert.Equal(t, result, "``- This is `code```")
}
7 changes: 7 additions & 0 deletions examples/gno.land/p/demo/table/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/p/demo/table

require (
gno.land/p/demo/uassert v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/urequire v0.0.0-latest
)
Loading
Loading