diff --git a/org/html_writer.go b/org/html_writer.go
index 7449a04..c1c6a15 100644
--- a/org/html_writer.go
+++ b/org/html_writer.go
@@ -622,7 +622,8 @@ func (w *HTMLWriter) blockContent(name string, children []Node) string {
WriteNodes(w, children...)
out := w.String()
w.Builder, w.htmlEscape = builder, htmlEscape
- return strings.TrimRightFunc(out, unicode.IsSpace)
+
+ return strings.TrimRightFunc(strings.TrimLeftFunc(out, IsNewLineChar), unicode.IsSpace)
} else {
return w.WriteNodesAsString(children...)
}
diff --git a/org/testdata/blocks.html b/org/testdata/blocks.html
index a020dbc..e55c978 100644
--- a/org/testdata/blocks.html
+++ b/org/testdata/blocks.html
@@ -22,6 +22,16 @@ block caption
+a source block with leading newline, trailing newline characters
+and a line started
+ with leading space
+ and line leading tab.
+
+
+
+
+
+
a source block without a language
diff --git a/org/testdata/blocks.org b/org/testdata/blocks.org
index b4fa908..d9ff6e5 100644
--- a/org/testdata/blocks.org
+++ b/org/testdata/blocks.org
@@ -12,6 +12,15 @@ function hello {
hello
#+END_SRC
+#+BEGIN_SRC
+
+a source block with leading newline, trailing newline characters
+and a line started
+ with leading space
+ and line leading tab.
+
+#+END_SRC
+
#+BEGIN_SRC
a source block without a language
#+END_SRC
diff --git a/org/testdata/blocks.pretty_org b/org/testdata/blocks.pretty_org
index 2fade9b..4ae5b0c 100644
--- a/org/testdata/blocks.pretty_org
+++ b/org/testdata/blocks.pretty_org
@@ -12,6 +12,15 @@ function hello {
hello
#+END_SRC
+#+BEGIN_SRC
+
+a source block with leading newline, trailing newline characters
+and a line started
+ with leading space
+ and line leading tab.
+
+#+END_SRC
+
#+BEGIN_SRC
a source block without a language
#+END_SRC
diff --git a/org/util.go b/org/util.go
index b83b6f4..74c361c 100644
--- a/org/util.go
+++ b/org/util.go
@@ -68,3 +68,7 @@ func ParseRanges(s string) [][2]int {
}
return ranges
}
+
+func IsNewLineChar(r rune) bool {
+ return r == '\n' || r == '\r'
+}