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): Add Gno Package for Markdown Rendering #2811

Closed
wants to merge 11 commits into from

Conversation

thanhngoc541
Copy link

@thanhngoc541 thanhngoc541 commented Sep 18, 2024

Related to #2753

This pull request introduces a new Gno package that utilizes the blackfriday library to render Markdown into HTML.

Supported Markdown Syntax:

  • Headers (#, ##, ###, etc.)
  • Lists (ordered and unordered)
  • Links and Images
  • Code blocks and inline code
  • Emphasis (bold and italic)
  • Blockquotes

Screenshot 2024-09-18 at 17 46 26
Screenshot 2024-09-18 at 17 46 33

func RenderExampleHeader() string {
	input := `
# Heading Level 1
## Heading Level 2
### Heading Level 3
#### Heading Level 4
##### Heading Level 5
###### Heading Level 6
`
	out := runMarkdown(input)
	return out
}
func RenderExampleLists() string {
	input := `
# Unordered List

* Item 1
    * Subitem 1.1
    	* Subitem 1.1.1
* Item 2
    * Subitem 2.1
    	* Subitem 2.1.1


# Ordered List

1. First item
   1. Subitem 1.1
      1. Subitem 1.1.1
2. Second item
   1. Subitem 2.1
      1. Subitem 2.1.1
`

	out := runMarkdown(input)

	return out
}
func RenderExampleLinkAndImage() string {
	input := `
# Link And Image
Here is a [link to VarMeta](https://www.var-meta.com/).
![Gno logo](https://www.var-meta.com/favicon-16x16.png)
`

	out := runMarkdown(input)

	return out
}
func RenderExampleCode() string {
	input := `# Code Block
Here is an inline code example: ` + "`" + `fmt.Println("Hello Gno!")` + "`" + `.

` + "``` go\n" + `func foo() bool {
	return true;
	}
` + "```\n"

	out := runMarkdown(input)

	return out
}
func RenderExampleEmphasis() string {
	input := `# Emphasis
This is **bold** text, and this is _italic_ text.
`
	out := runMarkdown(input)
	return out
}
func RenderExampleBlockquote() string {
	input := `# Blockquote #
> This is a blockquote.

> Here is another blockquote with **bold** and _italic_ text.
`

	out := runMarkdown(input)

	return out
}
func RenderExampleTable() string {
	input := `# Table
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1   | Cell 2   |
| Cell 3   | Cell 4   |
`
	out := runMarkdown(input)
	return out
}
image
Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

@thanhngoc541 thanhngoc541 requested review from a team as code owners September 18, 2024 04:49
@thanhngoc541 thanhngoc541 requested review from sw360cab and ltzmaxwell and removed request for a team September 18, 2024 04:49
@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Sep 18, 2024
@thanhngoc541 thanhngoc541 changed the title Add Gno Package for Markdown Rendering feat: Add Gno Package for Markdown Rendering Sep 18, 2024
@thanhngoc541 thanhngoc541 changed the title feat: Add Gno Package for Markdown Rendering feat(examples): Add Gno Package for Markdown Rendering Sep 18, 2024
@thinhnx-var
Copy link
Contributor

@moul @leohhhn
We are pushing more usage examples to indicate what this package can do.
Could you take a look on this PR and give us review if we are going into right approach or not?

@Kouteki Kouteki added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Oct 3, 2024
Copy link
Member

@notJoon notJoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package appears to be a port of github.com/russross/blackfriday.

Since the original repository uses a BSD license, you need to maintain the original copyright notice and license terms.

Please add a LICENSE file to the root directory of your project. Specifically, please add a file named 'LICENSE' in the root directory.

Copy link

codecov bot commented Oct 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.86%. Comparing base (01ee5a9) to head (caf6875).
Report is 35 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2811      +/-   ##
==========================================
+ Coverage   60.84%   60.86%   +0.01%     
==========================================
  Files         563      563              
  Lines       75190    75190              
==========================================
+ Hits        45753    45765      +12     
+ Misses      26068    26058      -10     
+ Partials     3369     3367       -2     
Flag Coverage Δ
contribs/gnodev 61.46% <ø> (+0.81%) ⬆️
contribs/gnofaucet 15.31% <ø> (+0.85%) ⬆️
gno.land 67.17% <ø> (ø)
gnovm 65.63% <ø> (ø)
misc/genstd 80.54% <ø> (ø)
misc/logos 19.88% <ø> (ø)
tm2 62.08% <ø> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@notJoon notJoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left some improvements, mostly they are slice range check relates. Please check and apply them. Thank you.

+) CI failing. Please run gno mod tidy on the markdown directory.

examples/gno.land/p/demo/varmeta_md/markdown.gno Outdated Show resolved Hide resolved
@@ -0,0 +1,5 @@
module gno.land/r/demo/varmeta_md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about changing the package name to md or markdown?

Comment on lines 46 to 47
data = data[offset:]
c := data[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing data[0] when the data slice is empty can cause a runtime panic. I think need to check the length of data to prevent accessing an empty slice.

Suggested change
data = data[offset:]
c := data[0]
data = data[offset:]
if len(data) == 0 {
return 0
}
c := data[0]

This pattern is also found in other functions (e.g., codeSpan). please check them just for safety.

Comment on lines 103 to 105
if data[0] != '#' {
return false
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add slice bound check here as well.

Suggested change
if data[0] != '#' {
return false
}
if len(data) == 0 || data[0] != '#' {
return false
}

c := data[i]

n := 0
for data[i] != '\n' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add index range check here

Suggested change
for data[i] != '\n' {
for i < len(data) && data[i] != '\n' {

examples/gno.land/p/demo/varmeta_md/block.gno Outdated Show resolved Hide resolved
@thanhngoc541 thanhngoc541 requested a review from thehowl as a code owner October 15, 2024 08:15
@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Oct 15, 2024
@thanhngoc541
Copy link
Author

I have update the pull request follow your comment. Thank for your suggestion!

@leohhhn
Copy link
Contributor

leohhhn commented Oct 15, 2024

Hey @thanhngoc541 - just like #2947, we don't really want to have a HTML library. The point of the Markdown UI bounty was to have a Go-like way to write markdown, instead of manually concatenating strings. This PR adds a processor that converts that already impractical string concatenation into HTML. We don't intend to have either.

Thank you for your effort, but you can close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🤖 gnovm Issues or PRs gnovm related 🧾 package/realm Tag used for new Realms or Packages. review/triage-pending PRs opened by external contributors that are waiting for the 1st review
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants