diff --git a/go.mod b/go.mod index 60b6818..08d7018 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,11 @@ module github.com/phc-dm/phc-server go 1.13 require ( + github.com/alecthomas/chroma v0.9.4 // indirect github.com/go-chi/chi/v5 v5.0.7 github.com/joho/godotenv v1.4.0 github.com/litao91/goldmark-mathjax v0.0.0-20210217064022-a43cf739a50f github.com/yuin/goldmark v1.4.4 + github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index 1a0f312..bea734a 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,48 @@ +github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI= +github.com/alecthomas/chroma v0.7.2-0.20200305040604-4f3623dce67a h1:3v1NrYWWqp2S72e4HLgxKt83B3l0lnORDholH/ihoMM= +github.com/alecthomas/chroma v0.7.2-0.20200305040604-4f3623dce67a/go.mod h1:fv5SzZPFJbwp2NXJWpFIX7DZS4HgV1K4ew4Pc2OZD9s= +github.com/alecthomas/chroma v0.9.4 h1:YL7sOAE3p8HS96T9km7RgvmsZIctqbK1qJ0b7hzed44= +github.com/alecthomas/chroma v0.9.4/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= +github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0= +github.com/alecthomas/kong v0.2.1-0.20190708041108-0548c6b1afae/go.mod h1:+inYUSluD+p4L8KdviBSgzcqEjUQOfC5fQDRFuc36lI= +github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= +github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ= +github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= +github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk= +github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= +github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= +github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/litao91/goldmark-mathjax v0.0.0-20210217064022-a43cf739a50f h1:plCPYXRXDCO57qjqegCzaVf1t6aSbgCMD+zfz18POfs= github.com/litao91/goldmark-mathjax v0.0.0-20210217064022-a43cf739a50f/go.mod h1:leg+HM7jUS84JYuY120zmU68R6+UeU6uZ/KAW7cViKE= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.6/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.4 h1:zNWRjYUW32G9KirMXYHQHVNFkXvMI7LpgNW2AgYAoIs= github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg= +github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 h1:0SJnXjE4jDClMW6grE0xpNhwpqbPwkBTn8zpVw5C0SI= +github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01/go.mod h1:TwKQPa5XkCCRC2GRZ5wtfNUTQ2+9/i19mGRijFeJ4BE= +golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/news.go b/news.go index a764eda..29cb8aa 100644 --- a/news.go +++ b/news.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "fmt" "os" "path" "sort" @@ -12,21 +13,50 @@ import ( "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/util" + chromahtml "github.com/alecthomas/chroma/formatters/html" mathjax "github.com/litao91/goldmark-mathjax" + highlighting "github.com/yuin/goldmark-highlighting" "gopkg.in/yaml.v3" ) var md goldmark.Markdown +// https://github.com/yuin/goldmark-highlighting/blob/9216f9c5aa010c549cc9fc92bb2593ab299f90d4/highlighting_test.go#L27 +func customWrapper(w util.BufWriter, c highlighting.CodeBlockContext, entering bool) { + lang, ok := c.Language() + if entering { + if ok { + w.WriteString(fmt.Sprintf(`
`, lang))
+ return
+ }
+ w.WriteString("")
+ } else {
+ if ok {
+ w.WriteString("
")
+ return
+ }
+ w.WriteString("
")
+ }
+}
+
func init() {
+
md = goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.Typographer,
// Questo pacchetto ha un nome stupido perché in realtà si occupa solo del parsing lato server del Markdown mentre lato client usiamo KaTeX.
mathjax.NewMathJax(),
+ highlighting.NewHighlighting(
+ highlighting.WithStyle("github"),
+ highlighting.WithWrapperRenderer(customWrapper),
+ highlighting.WithFormatOptions(
+ chromahtml.PreventSurroundingPre(true),
+ ),
+ ),
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
diff --git a/news/2021-12-22-notizia-1.md b/news/2021-12-22-notizia-1.md
index a629598..b0f46b3 100644
--- a/news/2021-12-22-notizia-1.md
+++ b/news/2021-12-22-notizia-1.md
@@ -23,7 +23,7 @@ $$
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repudiandae optio ad, consequatur distinctio possimus _laudantium molestias similique placeat_, dolore omnis et aperiam rem [delectus tempora ea,](#) cupiditate explicabo vel! Porro?
-![testing](https://picsum.photos/200/300)
+![testing](https://picsum.photos/400/300)
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repudiandae optio ad, consequatur distinctio possimus laudantium molestias similique placeat, dolore omnis et aperiam rem delectus tempora ea, cupiditate explicabo vel! Porro?
@@ -36,8 +36,17 @@ Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repudiandae optio ad,
- Item 5
- foo
- - bar
- ```Makefile
- foo
- foo
- ```
\ No newline at end of file
+ - bar with some `code`
+ ```go
+ type Article struct {
+ Id string
+ Title string
+ Description string
+ Tags []string
+ PublishDate time.Time
+ Important bool
+
+ SourceMarkdown string
+ OutputHTML string
+ }
+ ```
\ No newline at end of file
diff --git a/public/style.css b/public/style.css
index 75c7430..4c6a79d 100644
--- a/public/style.css
+++ b/public/style.css
@@ -4,6 +4,8 @@
:root {
--bg: #eaeaea;
--fg: #333;
+
+ --bg-lighter: #f0f0f0;
--bg-dark: hsl(220, 5%, 93%);
--bg-darker: hsl(220, 5%, 90%);
@@ -357,12 +359,30 @@ h4 {
font-weight: var(--font-weight-bold);
}
-p, ul, ol, li {
+p, ul, ol, li, pre {
margin: 0.5rem 0;
width: 70ch;
max-width: 100%;
}
+ul, ol {
+ padding: 0 0 0 1.5rem;
+}
+
+pre {
+ background: var(--bg-lighter);
+ border-radius: 2px;
+ border: 1px solid #cbcbcb;
+ box-shadow: 0 2px 4px 0 #00000033;
+
+ font-size: 90%;
+}
+
+pre > code {
+ display: block;
+ margin: 0.25rem;
+}
+
p.center {
text-align: center;
}
@@ -597,4 +617,36 @@ form .field-set input {
.page-utenti .search button {
width: 2.5rem;
height: 2.5rem;
-}
\ No newline at end of file
+}
+
+/* Rendered Markdown */
+
+.news-content {
+ display: block;
+}
+
+.news-content h1 {
+ text-align: center;
+}
+
+.news-content .math.display {
+ margin: 1rem 0;
+}
+
+.news-content img {
+ display: block;
+ margin: 1rem auto;
+
+ background: var(--bg-dark);
+ border-radius: 4px;
+ border: 1px solid var(--bg-darker-2);
+ box-shadow: 0 2px 8px 0 #00000033;
+}
+
+/* Math */
+
+.katex-display {
+ margin: 0;
+}
+
+
diff --git a/public/theme-dark.css b/public/theme-dark.css
index f094a61..637a357 100644
--- a/public/theme-dark.css
+++ b/public/theme-dark.css
@@ -60,4 +60,14 @@ body.dark-mode {
.dark-mode input[type] {
background: #4b4342;
+}
+
+.dark-mode pre {
+ background: var(--bg-darker);
+ border: 1px solid var(--bg-darker-2);
+}
+
+/* Trick molto molto malvagio per non dover rendere il colorscheme dei blocchi di codice dinamici rispetto alla dark mode */
+.dark-mode pre > code > span {
+ filter: invert(1);
}
\ No newline at end of file
diff --git a/views/news-base.html b/views/news-base.html
index 2e3203e..d19bdf1 100644
--- a/views/news-base.html
+++ b/views/news-base.html
@@ -3,7 +3,7 @@
{{define "title"}}PHC • phc.dm.xxxxx.xx{{end}}
{{define "body"}}
-