Skip to content

Commit

Permalink
text: partial cleanup in conditional cases
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Jul 22, 2019
1 parent 3128116 commit 26a0d9a
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 295 deletions.
45 changes: 43 additions & 2 deletions renderer.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"strings"
"unicode"

"github.com/MichaelMure/go-term-text"
"github.com/alecthomas/chroma"
Expand Down Expand Up @@ -292,9 +293,12 @@ func (r *renderer) RenderNode(w io.Writer, node *blackfriday.Node, entering bool
case blackfriday.Image:

case blackfriday.Text:
cleaned := strings.ReplaceAll(string(node.Literal), "\n", "")
content := string(node.Literal)
if shouldCleanText(node) {
content = removeLineBreak(content)
}
// emoji support !
emojed := emoji.Sprint(cleaned)
emojed := emoji.Sprint(content)
r.inlineAccumulator.WriteString(emojed)

case blackfriday.HTMLBlock:
Expand Down Expand Up @@ -416,3 +420,40 @@ func (r *renderer) renderFormattedCodeBlock(w io.Writer, code string) {

_, _ = fmt.Fprintf(w, "\n\n")
}

func removeLineBreak(text string) string {
lines := strings.Split(text, "\n")

if len(lines) <= 1 {
return text
}

for i, l := range lines {
switch i {
case 0:
lines[i] = strings.TrimRightFunc(l, unicode.IsSpace)
case len(lines) - 1:
lines[i] = strings.TrimLeftFunc(l, unicode.IsSpace)
default:
lines[i] = strings.TrimFunc(l, unicode.IsSpace)
}
}
return strings.Join(lines, " ")
}

func shouldCleanText(node *blackfriday.Node) bool {
for node != nil {
switch node.Type {
case blackfriday.BlockQuote:
return false

case blackfriday.Heading, blackfriday.Image, blackfriday.Link,
blackfriday.TableCell, blackfriday.Document, blackfriday.Item:
return true
}

node = node.Parent
}

panic("bad markdown document or missing case")
}
38 changes: 38 additions & 0 deletions renderer_test.go
@@ -0,0 +1,38 @@
package markdown

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRemoveLineBreak(t *testing.T) {
tests := []struct {
input string
want string
}{
{
"hello\nhello",
"hello hello",
},
{
"hello \nhello",
"hello hello",
},
{
"hello\n hello",
"hello hello",
},
{
" hello hello \n hello hello ",
" hello hello hello hello ",
},
{
" hello ",
" hello ",
},
}
for _, tt := range tests {
assert.Equal(t, tt.want, removeLineBreak(tt.input))
}
}
@@ -1,9 +1,8 @@
In Markdown 1.0.0 and earlier.
Version
8. This line turns into a list item.
Because a hard-wrapped line in the
middle of a paragraph looked like a
list item.
Version 8. This line turns into a
list item. Because a hard-wrapped
line in the middle of a paragraph
looked like a list item.

Here's one with a bullet.
* criminey.
Here's one with a bullet. *
criminey.
13 changes: 6 additions & 7 deletions testdata_result/Hard-wrapped paragraphs with list-like lines.txt
@@ -1,9 +1,8 @@
In Markdown 1.0.0 and earlier.
Version
8. This line turns into a list item.
Because a hard-wrapped line in the
middle of a paragraph looked like a
list item.
Version 8. This line turns into a
list item. Because a hard-wrapped
line in the middle of a paragraph
looked like a list item.

Here's one with a bullet.
* criminey.
Here's one with a bullet. *
criminey.
97 changes: 48 additions & 49 deletions testdata_result/Markdown Documentation - Basics.txt
Expand Up @@ -27,55 +27,55 @@
Formatting Syntax

This page offers a brief overview of
what it's like to use Markdown.
The [syntax
what it's like to use Markdown. The
[syntax
page](/projects/markdown/syntax
Markdown Syntax) provides complete,
detailed documentation for
every feature, but Markdown should
be very easy to pick up simply by
looking at a few examples of it in
action. The examples on this page
are written in a before/after style,
showing example syntax and the
HTML output produced by Markdown.
detailed documentation for every
feature, but Markdown should be very
easy to pick up simply by looking
at a few examples of it in action.
The examples on this page are
written in a before/after style,
showing example syntax and the HTML
output produced by Markdown.

It's also helpful to simply try
Markdown out; the
[Dingus](/projects/markdown/dingus
Markdown Dingus) is a
web application that allows you type
your own Markdown-formatted text
and translate it to XHTML.
Markdown Dingus) is a web
application that allows you type
your own Markdown-formatted text and
translate it to XHTML.

Note: This document is itself
written using Markdown; you
can [see the source for it by adding
'.text' to the URL](/projects/markd
own/basics.text).
written using Markdown; you can [see
the source for it by adding '.text'
to the URL](/projects/markdown/basi
cs.text).

 1.2 Paragraphs, Headers, Blockquotes

A paragraph is simply one or more
consecutive lines of text, separated
by one or more blank lines. (A blank
line is any line that looks like a
blank line -- a line containing
nothing spaces or tabs is considered
blank.) Normal paragraphs should not
be intended with spaces or tabs.
by one or more blank lines. (A
blank line is any line that looks
like a blank line -- a line
containing nothing spaces or tabs is
considered blank.) Normal
paragraphs should not be intended
with spaces or tabs.

Markdown offers two styles of
headers: Setext and atx.
Setext-style headers for <h1> and
<h2> are created by
"underlining" with equal signs (=)
and hyphens (-), respectively.
To create an atx-style header, you
put 1-6 hash marks (#) at the
beginning of the line -- the number
of hashes equals the resulting
HTML header level.
<h2> are created by "underlining"
with equal signs (=) and hyphens
(-), respectively. To create an
atx-style header, you put 1-6 hash
marks (#) at the beginning of the
line -- the number of hashes equals
the resulting HTML header level.

Blockquotes are indicated using
email-style '>' angle brackets.
Expand Down Expand Up @@ -170,8 +170,8 @@ Formatting Syntax
Unordered (bulleted) lists use
asterisks, pluses, and hyphens (*,
+, and -) as list markers. These
three markers are
interchangable; this:
three markers are interchangable;
this:

┃ * Candy.
┃ * Gum.
Expand Down Expand Up @@ -217,8 +217,8 @@ Formatting Syntax
items, you'll get <p> tags for the
list item text. You can create
multi-paragraph list items by
indenting
the paragraphs by 4 spaces or 1 tab:
indenting the paragraphs by 4 spaces
or 1 tab:

┃ * A list item.
┃ 
Expand All @@ -241,12 +241,12 @@ Formatting Syntax
Markdown supports two styles for
creating links: inline and
reference. With both styles, you use
square brackets to delimit the
text you want to turn into a link.
square brackets to delimit the text
you want to turn into a link.

Inline-style links use parentheses
immediately after the link text.
For example:
immediately after the link text. For
example:

┃ This is an [example
┃ link](http://example.com/).
Expand Down Expand Up @@ -345,11 +345,11 @@ Formatting Syntax
In a regular paragraph, you can
create code span by wrapping text in
backtick quotes. Any ampersands (&)
and angle brackets (< or
>) will automatically be translated
into HTML entities. This makes
it easy to use Markdown to write
about HTML example code:
and angle brackets (< or >) will
automatically be translated into
HTML entities. This makes it easy to
use Markdown to write about HTML
example code:

┃ I strongly recommend against using
┃ any `<blink>` tags.
Expand All @@ -375,10 +375,9 @@ Formatting Syntax

To specify an entire block of
pre-formatted code, indent every
line of
the block by 4 spaces or 1 tab. Just
like with code spans, &, <,
and > characters will be escaped
line of the block by 4 spaces or 1
tab. Just like with code spans, &,
<, and > characters will be escaped
automatically.

Markdown:
Expand Down

0 comments on commit 26a0d9a

Please sign in to comment.