Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front matter support #58

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bdaf895
add support for YAML frontmatter
juanolon Jun 3, 2024
e0bdb9e
Merge remote-tracking branch 'source/master' into meta
juanolon Jun 12, 2024
67e7aac
add Title to Page interface
juanolon Jun 12, 2024
853ebfc
remove logs
juanolon Jun 12, 2024
7005ee3
parse yaml directly
juanolon Jun 13, 2024
71ec102
add metadata extension. it strips yaml information from the top of th…
juanolon Jun 13, 2024
efa8f76
use the title attribute in template
juanolon Jun 13, 2024
304d04e
fix frontmatter strip, whenever a '---' was in the document
juanolon Jun 14, 2024
e6a0da5
implement GetMeta
juanolon Jun 14, 2024
ba55a6a
show hastags from frontmatter
juanolon Jun 14, 2024
1bc8b7b
parse yaml only once
juanolon Jun 15, 2024
e764fa5
support autolink, autocomplete
juanolon Jun 15, 2024
d143586
ModTime returns now date set in yaml
juanolon Jun 15, 2024
326c065
complete hashtags support
juanolon Jun 15, 2024
c62066c
finish related pages. partial was missing
juanolon Jun 15, 2024
5546397
fix wrong lookup on 'see also' section
juanolon Jun 15, 2024
dd9d338
remove comment
juanolon Jul 1, 2024
e1f0f61
Merge remote-tracking branch 'source/master' into meta
juanolon Jul 1, 2024
700ec79
remove ModTime parameter
emad-elsaid Jul 6, 2024
bcd838d
rename Getmeta to Metadata to remove the Get part and make it identic…
emad-elsaid Jul 6, 2024
8468c11
remove modtime param from templates
emad-elsaid Jul 6, 2024
5ea4f87
set modtime if we successfully got the file stat
emad-elsaid Jul 6, 2024
f5fc550
remove printf
emad-elsaid Jul 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion extensions/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
_ "github.com/emad-elsaid/xlog/extensions/gpg"
_ "github.com/emad-elsaid/xlog/extensions/hashtags"
_ "github.com/emad-elsaid/xlog/extensions/heading"
_ "github.com/emad-elsaid/xlog/extensions/hotreload"
_ "github.com/emad-elsaid/xlog/extensions/html"
_ "github.com/emad-elsaid/xlog/extensions/images"
_ "github.com/emad-elsaid/xlog/extensions/link_preview"
_ "github.com/emad-elsaid/xlog/extensions/manifest"
_ "github.com/emad-elsaid/xlog/extensions/mathjax"
_ "github.com/emad-elsaid/xlog/extensions/mermaid"
_ "github.com/emad-elsaid/xlog/extensions/metadata"
_ "github.com/emad-elsaid/xlog/extensions/opengraph"
_ "github.com/emad-elsaid/xlog/extensions/pandoc"
_ "github.com/emad-elsaid/xlog/extensions/photos"
Expand All @@ -34,5 +36,4 @@ import (
_ "github.com/emad-elsaid/xlog/extensions/todo"
_ "github.com/emad-elsaid/xlog/extensions/upload_file"
_ "github.com/emad-elsaid/xlog/extensions/versions"
_ "github.com/emad-elsaid/xlog/extensions/hotreload"
)
4 changes: 2 additions & 2 deletions extensions/autolink_pages/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (a autocomplete) Suggestions() []*Suggestion {

EachPage(context.Background(), func(p Page) {
suggestions = append(suggestions, &Suggestion{
Text: p.Name(),
DisplayText: "@" + p.Name(),
Text: p.Title(),
DisplayText: "@" + p.Title(),
})
})

Expand Down
5 changes: 3 additions & 2 deletions extensions/autolink_pages/autolink_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type fileInfoByNameLength []Page

func (a fileInfoByNameLength) Len() int { return len(a) }
func (a fileInfoByNameLength) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a fileInfoByNameLength) Less(i, j int) bool { return len(a[i].Name()) > len(a[j].Name()) }
func (a fileInfoByNameLength) Less(i, j int) bool { return len(a[i].Title()) > len(a[j].Title()) }

func init() {
Listen(AfterWrite, UpdatePagesList)
Expand Down Expand Up @@ -79,7 +79,8 @@ func backlinksSection(p Page) template.HTML {
func containLinkTo(n ast.Node, p Page) bool {
if n.Kind() == KindPageLink {
t, _ := n.(*PageLink)
if t.page.FileName() == p.FileName() {
// log.Printf("link %s", t.page.FileName())
if t.page.Title() == p.Title() {
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/autolink_pages/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (_ *PageLink) Kind() ast.NodeKind {

func (p *PageLink) Dump(source []byte, level int) {
m := map[string]string{
"value": p.page.Name(),
"value": p.page.Title(),
}
ast.DumpHelper(p, source, level, m, nil)
}
6 changes: 3 additions & 3 deletions extensions/autolink_pages/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func (s *pageLinkParser) Parse(parent ast.Node, block text.Reader, pc parser.Con
var m int

for _, p := range autolinkPages {
if len(line) < len(p.Name()) {
if len(line) < len(p.Title()) {
continue
}

// Found a page
if strings.EqualFold(string(line[0:len(p.Name())]), p.Name()) {
if strings.EqualFold(string(line[0:len(p.Title())]), p.Title()) {
found = p
m = len(p.Name())
m = len(p.Title())
break
}
}
Expand Down
5 changes: 5 additions & 0 deletions extensions/gpg/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ type page struct {
}

func (p *page) Name() string { return p.name }
func (p *page) Title() string { return p.name }
func (p *page) FileName() string { return filepath.FromSlash(p.name) + EXT }

func (p *page) Metadata() (xlog.Metadata, bool) {
return xlog.Metadata{}, false
}

func (p *page) Exists() bool {
_, err := os.Stat(p.FileName())
return err == nil
Expand Down
50 changes: 44 additions & 6 deletions extensions/hashtags/hashtags.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,24 @@ func tagsHandler(_ Response, r Request) Output {
var lck sync.Mutex

EachPageCon(context.Background(), func(a Page) {

set := map[string]bool{}
_, tree := a.AST()
hashes := FindAllInAST[*HashTag](tree)

for _, v := range hashes {
val := strings.ToLower(string(v.value))
set[val] = true
}

// don't use same tag twice for same page
if _, ok := set[val]; ok {
continue
meta, ok := a.Metadata()
if ok {
for _, v := range meta.Tags {
val := strings.ToLower(v)
set[val] = true
}

set[val] = true

}
for val := range set {
lck.Lock()
if ps, ok := tags[val]; ok {
tags[val] = append(ps, a)
Expand Down Expand Up @@ -163,6 +167,15 @@ func tagPages(ctx context.Context, keyword string) []*Page {
}
}

meta, ok := p.Metadata()
if ok {
for _, v := range meta.Tags {
if strings.EqualFold(v, keyword) {
return &p
}
}
}

return nil
})
}
Expand All @@ -178,6 +191,12 @@ func relatedPages(p Page) template.HTML {
for _, v := range found_hashtags {
hashtags[strings.ToLower(string(v.value))] = true
}
meta, ok := p.Metadata()
if ok {
for _, v := range meta.Tags {
hashtags[strings.ToLower(v)] = true
}
}

pages := MapPageCon(context.Background(), func(rp Page) *Page {
if rp.Name() == p.Name() {
Expand All @@ -192,6 +211,15 @@ func relatedPages(p Page) template.HTML {
}
}

meta, ok := rp.Metadata()
if ok {
for _, v := range meta.Tags {
if _, ok := hashtags[strings.ToLower(v)]; ok {
return &rp
}
}
}

return nil
})

Expand All @@ -214,6 +242,16 @@ func (a autocomplete) Suggestions() []*Suggestion {
EachPageCon(context.Background(), func(a Page) {
_, tree := a.AST()
hashes := FindAllInAST[*HashTag](tree)

meta, ok := a.Metadata()
if ok {
lck.Lock()
for _, v := range meta.Tags {
set[strings.ToLower(v)] = true
}
lck.Unlock()
}

lck.Lock()
defer lck.Unlock()
for _, v := range hashes {
Expand Down
8 changes: 8 additions & 0 deletions extensions/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ func (p *page) Name() string {
return p.name
}

func (p *page) Title() string {
return p.name
}

func (p *page) Metadata() (xlog.Metadata, bool) {
return xlog.Metadata{}, false
}

func (p *page) FileName() string {
return filepath.FromSlash(p.name) + p.ext
}
Expand Down
64 changes: 64 additions & 0 deletions extensions/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package metadata

import (
"bufio"
"strings"

"github.com/emad-elsaid/xlog"
. "github.com/emad-elsaid/xlog"
"gopkg.in/yaml.v2"
)

func init() {
RegisterPreprocessor(stripYAML)
}

func stripYAML(content Markdown) Markdown {
reader := strings.NewReader(string(content))

scanner := bufio.NewScanner(reader)
var body strings.Builder
var y strings.Builder
inFrontMatter := false
frontMatterFound := false

for scanner.Scan() {
line := scanner.Text()

if strings.TrimSpace(line) == "---" {
emad-elsaid marked this conversation as resolved.
Show resolved Hide resolved
if inFrontMatter {
// End of front matter
inFrontMatter = false
frontMatterFound = true
} else if !frontMatterFound {
// Start of front matter
inFrontMatter = true
} else {
body.WriteString(line)
body.WriteString("\n")
}
} else {
if inFrontMatter {
y.WriteString(line)
y.WriteString("\n")
} else if !inFrontMatter {
body.WriteString(line)
body.WriteString("\n")
}

}
}

if err := scanner.Err(); err != nil {
// ignore error, and just return original content
return content
}

var meta xlog.Metadata
// only strip if valid yaml
err := yaml.Unmarshal([]byte(y.String()), &meta)
emad-elsaid marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return content
}
return Markdown(body.String())
}
8 changes: 8 additions & 0 deletions extensions/pandoc/pandoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func (p *page) Name() string {
return p.name
}

func (p *page) Title() string {
return p.name
}

func (p *page) Metadata() (xlog.Metadata, bool) {
return xlog.Metadata{}, false
}

func (p *page) FileName() string {
return filepath.FromSlash(p.name) + p.ext
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ require (
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/rjeczalik/notify v0.9.3
golang.org/x/sync v0.6.0
gopkg.in/yaml.v2 v2.3.0
)

require golang.org/x/net v0.23.0 // indirect
require golang.org/x/net v0.22.0 // indirect

require (
github.com/ProtonMail/go-crypto v1.0.0 // indirect
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI=
golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -123,5 +123,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
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.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func getPageHandler(w Response, r Request) Output {
}

return Render("view", Locals{
"title": page.Emoji() + " " + page.Name(),
"title": page.Emoji() + " " + page.Title(),
"page": page,
"edit": "/edit/" + page.Name(),
"content": page.Render(),
Expand All @@ -96,7 +96,7 @@ func getPageEditHandler(w Response, r Request) Output {
}

return Render("edit", Locals{
"title": page.Emoji() + " " + page.Name(),
"title": page.Emoji() + " " + page.Title(),
"page": page,
"commands": Commands(page),
"content": content,
Expand Down
Loading