snapshot of https://github.com/go-gitea/gitea.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
This commit is contained in:
103
modules/markup/markdown/meta.go
Normal file
103
modules/markup/markdown/meta.go
Normal file
@@ -0,0 +1,103 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package markdown
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
func isYAMLSeparator(line []byte) bool {
|
||||
idx := 0
|
||||
for ; idx < len(line); idx++ {
|
||||
if line[idx] >= utf8.RuneSelf {
|
||||
r, sz := utf8.DecodeRune(line[idx:])
|
||||
if !unicode.IsSpace(r) {
|
||||
return false
|
||||
}
|
||||
idx += sz
|
||||
continue
|
||||
}
|
||||
if line[idx] != ' ' {
|
||||
break
|
||||
}
|
||||
}
|
||||
dashCount := 0
|
||||
for ; idx < len(line); idx++ {
|
||||
if line[idx] != '-' {
|
||||
break
|
||||
}
|
||||
dashCount++
|
||||
}
|
||||
if dashCount < 3 {
|
||||
return false
|
||||
}
|
||||
for ; idx < len(line); idx++ {
|
||||
if line[idx] >= utf8.RuneSelf {
|
||||
r, sz := utf8.DecodeRune(line[idx:])
|
||||
if !unicode.IsSpace(r) {
|
||||
return false
|
||||
}
|
||||
idx += sz
|
||||
continue
|
||||
}
|
||||
if line[idx] != ' ' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ExtractMetadata consumes a markdown file, parses YAML frontmatter,
|
||||
// and returns the frontmatter metadata separated from the markdown content
|
||||
func ExtractMetadata(contents string, out any) (string, error) {
|
||||
body, err := ExtractMetadataBytes([]byte(contents), out)
|
||||
return string(body), err
|
||||
}
|
||||
|
||||
// ExtractMetadataBytes consumes a Markdown content, parses YAML frontmatter,
|
||||
// and returns the frontmatter metadata separated from the Markdown content
|
||||
func ExtractMetadataBytes(contents []byte, out any) ([]byte, error) {
|
||||
var front, body []byte
|
||||
|
||||
start, end := 0, len(contents)
|
||||
idx := bytes.IndexByte(contents[start:], '\n')
|
||||
if idx >= 0 {
|
||||
end = start + idx
|
||||
}
|
||||
line := contents[start:end]
|
||||
|
||||
if !isYAMLSeparator(line) {
|
||||
return contents, errors.New("frontmatter must start with a separator line")
|
||||
}
|
||||
frontMatterStart := end + 1
|
||||
for start = frontMatterStart; start < len(contents); start = end + 1 {
|
||||
end = len(contents)
|
||||
idx := bytes.IndexByte(contents[start:], '\n')
|
||||
if idx >= 0 {
|
||||
end = start + idx
|
||||
}
|
||||
line := contents[start:end]
|
||||
if isYAMLSeparator(line) {
|
||||
front = contents[frontMatterStart:start]
|
||||
if end+1 < len(contents) {
|
||||
body = contents[end+1:]
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(front) == 0 {
|
||||
return contents, errors.New("could not determine metadata")
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(front, out); err != nil {
|
||||
return contents, err
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
Reference in New Issue
Block a user