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 16 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
6 changes: 3 additions & 3 deletions extensions/activitypub/activitypub.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,12 @@ func outboxPage(w Response, r Request) Output {
ID: u.String(),
Type: "Create",
Actor: fmt.Sprintf("https://%s/+/activitypub/@%s", domain, username),
Published: page.ModTime(),
Published: page.ModTime(false),
To: []string{"https://www.w3.org/ns/activitystreams#Public"},
Object: outboxPageObject{
ID: u.String(),
Type: "Note",
Published: page.ModTime(),
Published: page.ModTime(false),
URL: u.String(),
AttributedTo: fmt.Sprintf("https://%s/+/activitypub/@%s", domain, username),
To: []string{"https://www.w3.org/ns/activitystreams#Public"},
Expand All @@ -264,4 +264,4 @@ type orderedPages []Page

func (a orderedPages) Len() int { return len(a) }
func (a orderedPages) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a orderedPages) Less(i, j int) bool { return a[i].ModTime().After(a[j].ModTime()) }
func (a orderedPages) Less(i, j int) bool { return a[i].ModTime(false).After(a[j].ModTime(false)) }
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
30 changes: 28 additions & 2 deletions extensions/autolink_pages/autolink_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"embed"
"html/template"
"path"
"sort"
"strings"
"sync"

_ "embed"
Expand All @@ -21,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 @@ -77,10 +79,34 @@ 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
}
}
if n.Kind() == ast.KindLink {
t, _ := n.(*ast.Link)
// log.Printf("link %s", t.Destination)
dst := string(t.Destination)

// link is absolute: remove /
if strings.HasPrefix(dst, "/") {
path := strings.TrimPrefix(dst, "/")
if string(path) == p.Name() {
return true
}
} else { // link is relative: get relative part
// TODO: what if another folder has the same filename?
// * just ignore that fact
// * dont support relative paths
// there is no way to know who is the parent folder
// log.Printf("-> relative link %s", dst)
base := path.Base(p.Name())
if dst == base {
return true
}
}
}

for c := n.FirstChild(); c != nil; c = c.NextSibling() {
if containLinkTo(c, p) {
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
7 changes: 6 additions & 1 deletion 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) GetMeta() (xlog.Metadata, bool) {
return xlog.Metadata{}, false
}

func (p *page) Exists() bool {
_, err := os.Stat(p.FileName())
return err == nil
Expand All @@ -51,7 +56,7 @@ func (p *page) Content() xlog.Markdown {
return xlog.Markdown(out)
}

func (p *page) ModTime() time.Time {
func (p *page) ModTime(real bool) time.Time {
s, err := os.Stat(p.FileName())
if err != nil {
return time.Time{}
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.GetMeta()
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.GetMeta()
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.GetMeta()
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.GetMeta()
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.GetMeta()
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
10 changes: 9 additions & 1 deletion 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) GetMeta() (xlog.Metadata, bool) {
return xlog.Metadata{}, false
}

func (p *page) FileName() string {
return filepath.FromSlash(p.name) + p.ext
}
Expand All @@ -107,7 +115,7 @@ func (p *page) Content() xlog.Markdown {
return xlog.Markdown(dat)
}

func (p *page) ModTime() time.Time {
func (p *page) ModTime(real bool) time.Time {
s, err := os.Stat(p.FileName())
if err != nil {
return time.Time{}
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())
}
10 changes: 9 additions & 1 deletion 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) GetMeta() (xlog.Metadata, bool) {
return xlog.Metadata{}, false
}

func (p *page) FileName() string {
return filepath.FromSlash(p.name) + p.ext
}
Expand Down Expand Up @@ -115,7 +123,7 @@ func (p *page) Content() xlog.Markdown {
return xlog.Markdown(dat)
}

func (p *page) ModTime() time.Time {
func (p *page) ModTime(real bool) time.Time {
s, err := os.Stat(p.FileName())
if err != nil {
return time.Time{}
Expand Down
2 changes: 1 addition & 1 deletion extensions/recent/recent.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type recentPages []Page

func (a recentPages) Len() int { return len(a) }
func (a recentPages) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a recentPages) Less(i, j int) bool { return a[i].ModTime().After(a[j].ModTime()) }
func (a recentPages) Less(i, j int) bool { return a[i].ModTime(false).After(a[j].ModTime(false)) }

type links int

Expand Down
4 changes: 2 additions & 2 deletions extensions/rss/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func feed(w Response, r Request) Output {

pages := Pages(r.Context())
sort.Slice(pages, func(i, j int) bool {
return pages[i].ModTime().After(pages[j].ModTime())
return pages[i].ModTime(false).After(pages[j].ModTime(false))
})

if len(pages) > limit {
Expand All @@ -92,7 +92,7 @@ func feed(w Response, r Request) Output {
f.Channel.Items = append(f.Channel.Items, Item{
Title: p.Name(),
Description: string(p.Render()),
PubDate: p.ModTime(),
PubDate: p.ModTime(false),
GUID: p.Name(),
Link: (&url.URL{
Scheme: "https",
Expand Down
Loading