Skip to content

Commit

Permalink
better styling and extract # header as page title
Browse files Browse the repository at this point in the history
  • Loading branch information
vimeh committed Sep 11, 2023
1 parent 82da2a9 commit 5c065af
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
31 changes: 23 additions & 8 deletions main.go
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"net/http"
"os"
"regexp"
"strings"

"github.com/gomarkdown/markdown"
"github.com/microcosm-cc/bluemonday"
Expand All @@ -17,6 +19,8 @@ const (
htmlFilePath = "rendered.html"
)

var pageTitle string

func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
file, _, err := r.FormFile("file")
Expand Down Expand Up @@ -46,6 +50,15 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
}
htmlContent := markdown.ToHTML(mdBytes, nil, nil)

// Extract first top-level heading as the page title
mdLines := strings.Split(string(mdBytes), "\n")
for _, line := range mdLines {
if strings.HasPrefix(line, "# ") {
pageTitle = strings.TrimPrefix(line, "# ")
break
}
}

htmlFile, err := os.Create(htmlFilePath)
if err != nil {
http.Error(w, "Unable to create HTML file", http.StatusInternalServerError)
Expand All @@ -72,17 +85,19 @@ func renderHandler(w http.ResponseWriter, r *http.Request) {
return
}

w.Write([]byte(`<!DOCTYPE html>
<html>
<head>
<title>Markdown Rendered as HTML</title>
<link rel="stylesheet" type="text/css" href="/static/styles.css">
</head>
<body>`))
w.Write([]byte(fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
<title>%s</title>
<link rel="stylesheet" type="text/css" href="/static/styles.css">
</head>
<body>
<div class="container">
`, pageTitle)))

w.Write(htmlContent)

w.Write([]byte(`</body>
w.Write([]byte(`</div></body>
</html>`))
}

Expand Down
10 changes: 6 additions & 4 deletions styles.css
Expand Up @@ -2,7 +2,7 @@

/* Global container for content */
.container {
max-width: 600px; /* Set to desired width, 600px is commonly recommended for emails */
max-width: 700px; /* Set to desired width, 600px is commonly recommended for emails */
margin-left: auto;
margin-right: auto;
padding: 0px;
Expand All @@ -17,11 +17,15 @@ h4,
h5,
h6 {
font-family: monospace;
font-size: 14px;
font-size: 18px;
line-height: 1.6;
padding: 0px;
}

h1 {
font-size: 32px;
}

/* Headers */
h1,
h2,
Expand All @@ -34,7 +38,6 @@ h6 {

h1 {
font-weight: bold;
border-bottom: 2px solid #ddd;
}
h2 {
font-weight: bold;
Expand Down Expand Up @@ -98,4 +101,3 @@ hr {
border: 1px solid #ddd;
margin: 0px 0;
}

0 comments on commit 5c065af

Please sign in to comment.