diff --git a/CHANGELOG.md b/CHANGELOG.md
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # Revision history for scripths
 
+## 0.1.0.1 -- 2026-02-24
+
+* Fix code new ine behaviour for code fencing.
+
 ## 0.1.0.0 -- YYYY-mm-dd
 
 * First version. Released on an unsuspecting world.
diff --git a/app/Main.hs b/app/Main.hs
--- a/app/Main.hs
+++ b/app/Main.hs
@@ -1,6 +1,6 @@
 module Main where
 
-import Data.List ( isPrefixOf )
+import Data.List (isPrefixOf)
 import qualified Data.Text.IO as TIO
 import System.Environment (getArgs, getProgName)
 import System.Exit (exitFailure)
diff --git a/scripths.cabal b/scripths.cabal
--- a/scripths.cabal
+++ b/scripths.cabal
@@ -1,6 +1,6 @@
 cabal-version:      3.0
 name:               scripths
-version:            0.1.0.0
+version:            0.1.0.1
 synopsis:           GHCi scripts for standalone execution and Markdown documentation.
 description:        GHCi scripts for standalone execution (with dependency resolution) and Markdown documentation (produces inline output).
 homepage:           https://www.datahaskell.org/
diff --git a/src/ScriptHs/Markdown.hs b/src/ScriptHs/Markdown.hs
--- a/src/ScriptHs/Markdown.hs
+++ b/src/ScriptHs/Markdown.hs
@@ -35,7 +35,13 @@
 fence = "```"
 
 fenceCodeSegment :: Text -> Text -> Text
-fenceCodeSegment lang output = "\n" <> fence <> lang <> "\n" <> output <> fence <> "\n"
+fenceCodeSegment lang output
+    | T.null output = ""
+    | otherwise = T.unlines ["", fence <> lang, ensureNewLine output, fence, ""]
+  where
+    ensureNewLine t
+        | "\n" `T.isSuffixOf` t = t
+        | otherwise = T.snoc t '\n'
 
 reassemble :: [Segment] -> Text
 reassemble = T.concat . map renderSegment
