Skip to content

Commit

Permalink
md中可以使用樣版導入css或者其他md內容 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonSlovoka committed Sep 26, 2024
1 parent bc92ddb commit 4e0dc54
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 3 deletions.
14 changes: 14 additions & 0 deletions assets/end_slides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<style>
.top_right {
position: fixed!important;
top: 1em;
right: 1em;
}
</style>

<span id="end"></span>

<section data-background-size="60%" data-background-image="{{.Img}}">

<a href="{{.Href}}" class="top_right">🎉</a>
</section>
3 changes: 3 additions & 0 deletions assets/fragment.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:is(li, a, span).current-fragment {
color: #ff5a00;
}
Binary file added assets/home2.webp
Binary file not shown.
11 changes: 11 additions & 0 deletions assets/pre.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.highlight-line .hljs-comment {
color: #70ff07 !important;
background-color: #000 !important;
font-style: italic;
font-size: large;
}

.highlight-line .hljs-ln-code {
color: yellow !important;
font-size: large;
}
5 changes: 5 additions & 0 deletions internal/tmpl/funcs/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package funcs
import (
"errors"
"reflect"
"strings"
)

func Dict(values ...any) (map[string]any, error) {
Expand Down Expand Up @@ -53,3 +54,7 @@ func List(args ...any) any {
}
return slice.Interface() // {interface() | []yourType
}

func Join(sep string, s ...string) string {
return strings.Join(s, sep)
}
51 changes: 51 additions & 0 deletions internal/tmpl/funcs/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package funcs

import (
"bytes"
"io"
"net/http"
"text/template"
)

func TxtFrom(srcs ...string) (string, error) {
buf := bytes.NewBuffer(nil)
for _, src := range srcs {
resp, err := http.Get(src)
if err != nil {
return "", err
}
var body []byte
body, err = io.ReadAll(resp.Body)
if err != nil {
return "", err
}
buf.Write(body)
_ = resp.Body.Close()
}
return buf.String(), nil
}

// Tmpl 注意,此template不提供額外的FuncMap
func Tmpl(src string, ctx map[string]any) (string, error) {
resp, err := http.Get(src)
if err != nil {
return "", err
}
var body []byte
body, err = io.ReadAll(resp.Body)
if err != nil {
return "", err
}
_ = resp.Body.Close()

var t *template.Template
t, err = template.New("").Parse(string(body))
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
if err = t.Execute(buf, ctx); err != nil {
return "", err
}
return buf.String(), nil
}
19 changes: 16 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ func HandleListMD(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, html)
}

func getBaseURL(r *http.Request) string {
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
return fmt.Sprintf("%s://%s", scheme, r.Host)
}

// HandleTxt 回傳md目錄下的檔案內容,視為純文本
func HandleTxt(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
Expand All @@ -127,8 +135,11 @@ func HandleTxt(w http.ResponseWriter, r *http.Request) {

// 用go template處理之後再返回
maps := funcs.Maps{
"dict": funcs.Dict,
"list": funcs.List, // slice 已經是保留字了,所以用list
"dict": funcs.Dict,
"list": funcs.List, // slice 已經是保留字了,所以用list
"join": funcs.Join,
"txtFrom": funcs.TxtFrom,
"tmpl": funcs.Tmpl,
}.AddMaps(funcs.MathMaps())
tmpl := textTemplate.New("").Funcs(textTemplate.FuncMap(maps))
tmpl, err = tmpl.Parse(string(bs))
Expand All @@ -137,7 +148,9 @@ func HandleTxt(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = tmpl.Execute(w, nil); err != nil {
if err = tmpl.Execute(w, map[string]any{
"BaseURL": getBaseURL(r),
}); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
47 changes: 47 additions & 0 deletions md/test_txtfrom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{{printf "<style>%s</style>" (txtFrom
(printf "%s%s" .BaseURL "/assets/pre.css")
(printf "%s%s" .BaseURL "/assets/fragment.css")
)
}}

```go [|2|3-5|7|8|7-8]
type VheaTable struct {
Version uint32 // 0x00010000 or 0x00110000
Ascent int16
Descent int16
LineGap int16 // Reserved; set to 0
AHMax uint16

MinTSB int16
MinBSB int16
YMaxExtent int16
CaretSlopeRise int16
CaretSlopeRun int16
CaretOffset int16
reserved1 int16
reserved2 int16
reserved3 int16
reserved4 int16
MetricDataFormat int16

NumMetrics uint16
}

```

---

- This is an <span class="fragment grow">apple</span>
- This is a <span class="fragment grow">pen</span>

Note:

透過fragment.css達成grow時顏色修改 `:is(li, a, span).current-fragment {color: #ff5a00;}`

---

{{tmpl (join "" .BaseURL "/assets/end_slides.md") (dict "Img" "/assets/home2.webp" "Href" "/md/example.md?theme=sky#3/2")}}

Note:

透過這種方式可以將指定的樣板匯入

0 comments on commit 4e0dc54

Please sign in to comment.