Skip to content

Commit

Permalink
Add Additional Text Formatting and Structure Elements
Browse files Browse the repository at this point in the history
  • Loading branch information
chasefleming committed Oct 7, 2023
1 parent eb0260b commit 35989dc
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,27 @@ In this example, if `isAdmin` is `true`, the `Admin Panel` link is rendered. Oth
`elem` provides utility functions for creating common HTML elements:

- `A`: Anchor `<a>`
- `Blockquote`: Blockquote `<blockquote>`
- `Body`: Body `<body>`
- `Br`: Break `<br>`
- `Button`: Button `<button>`
- `Code`: Code `<code>`
- `Div`: Division `<div>`
- `Em`: Emphasis `<em>`
- `H1`: Heading 1 `<h1>`
- `H2`: Heading 2 `<h2>`
- `H3`: Heading 3 `<h3>`
- `Head`: Head `<head>`
- `Html`: HTML `<html>`
- `Hr`: Horizontal Rule `<hr>`
- `Img`: Image `<img>`
- `Li`: List Item `<li>`
- `Meta`: Meta `<meta>`
- `P`: Paragraph `<p>`
- `Pre`: Preformatted Text `<pre>`
- `Script`: Script `<script>`
- `Span`: Span `<span>`
- `Strong`: Strong `<strong>`
- `Title`: Title `<title>`
- `Ul`: Unordered List `<ul>`

Expand Down
28 changes: 28 additions & 0 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,26 @@ func A(props Attrs, children ...interface{}) *Element {
return NewElement("a", props, children...)
}

func Br(props Attrs) *Element {
return NewElement("br", props)
}

func Blockquote(props Attrs, children ...interface{}) *Element {
return NewElement("blockquote", props, children...)
}

func Code(props Attrs, children ...interface{}) *Element {
return NewElement("code", props, children...)
}

func Div(props Attrs, children ...interface{}) *Element {
return NewElement("div", props, children...)
}

func Em(props Attrs, children ...interface{}) *Element {
return NewElement("em", props, children...)
}

func H1(props Attrs, children ...interface{}) *Element {
return NewElement("h1", props, children...)
}
Expand All @@ -40,14 +56,26 @@ func H3(props Attrs, children ...interface{}) *Element {
return NewElement("h3", props, children...)
}

func Hr(props Attrs) *Element {
return NewElement("hr", props)
}

func P(props Attrs, children ...interface{}) *Element {
return NewElement("p", props, children...)
}

func Pre(props Attrs, children ...interface{}) *Element {
return NewElement("pre", props, children...)
}

func Span(props Attrs, children ...interface{}) *Element {
return NewElement("span", props, children...)
}

func Strong(props Attrs, children ...interface{}) *Element {
return NewElement("strong", props, children...)
}

func Text(content string) string {
return content
}
Expand Down
42 changes: 42 additions & 0 deletions elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,36 @@ func TestA(t *testing.T) {
assert.Equal(t, expected, el.Render())
}

func TestBlockquote(t *testing.T) {
expected := `<blockquote>Quote text</blockquote>`
el := Blockquote(nil, Text("Quote text"))
assert.Equal(t, expected, el.Render())
}

func TestBr(t *testing.T) {
expected := `<br>`
el := Br(nil)
assert.Equal(t, expected, el.Render())
}

func TestCode(t *testing.T) {
expected := `<code>Code snippet</code>`
el := Code(nil, Text("Code snippet"))
assert.Equal(t, expected, el.Render())
}

func TestDiv(t *testing.T) {
expected := `<div class="container">Hello, Elem!</div>`
el := Div(Attrs{attrs.Class: "container"}, Text("Hello, Elem!"))
assert.Equal(t, expected, el.Render())
}

func TestEm(t *testing.T) {
expected := `<em>Italic text</em>`
el := Em(nil, Text("Italic text"))
assert.Equal(t, expected, el.Render())
}

func TestH1(t *testing.T) {
expected := `<h1 class="title">Hello, Elem!</h1>`
el := H1(Attrs{attrs.Class: "title"}, Text("Hello, Elem!"))
Expand All @@ -62,18 +86,36 @@ func TestH3(t *testing.T) {
assert.Equal(t, expected, el.Render())
}

func TestHr(t *testing.T) {
expected := `<hr>`
el := Hr(nil)
assert.Equal(t, expected, el.Render())
}

func TestP(t *testing.T) {
expected := `<p>Hello, Elem!</p>`
el := P(nil, Text("Hello, Elem!"))
assert.Equal(t, expected, el.Render())
}

func TestPre(t *testing.T) {
expected := `<pre>Preformatted text</pre>`
el := Pre(nil, Text("Preformatted text"))
assert.Equal(t, expected, el.Render())
}

func TestSpan(t *testing.T) {
expected := `<span class="highlight">Hello, Elem!</span>`
el := Span(Attrs{attrs.Class: "highlight"}, Text("Hello, Elem!"))
assert.Equal(t, expected, el.Render())
}

func TestStrong(t *testing.T) {
expected := `<strong>Bold text</strong>`
el := Strong(nil, Text("Bold text"))
assert.Equal(t, expected, el.Render())
}

// ========== Lists ==========

func TestLi(t *testing.T) {
Expand Down

0 comments on commit 35989dc

Please sign in to comment.