parseDocsFlattenFrontMatter extracts the title from a YAML (---) or TOML (+++) front-matter block and returns the body without it. Quoted and unquoted titles are both supported.
(data string)
| 677 | // (+++) front-matter block and returns the body without it. Quoted and |
| 678 | // unquoted titles are both supported. |
| 679 | func parseDocsFlattenFrontMatter(data string) (title, body string, hasFM bool) { |
| 680 | lines := strings.Split(data, "\n") |
| 681 | if len(lines) == 0 { |
| 682 | return "", data, false |
| 683 | } |
| 684 | fence := strings.TrimSpace(lines[0]) |
| 685 | var titleRe *regexp.Regexp |
| 686 | switch fence { |
| 687 | case "---": |
| 688 | titleRe = docsFlattenTitleYAML |
| 689 | case "+++": |
| 690 | titleRe = docsFlattenTitleTOML |
| 691 | default: |
| 692 | return "", data, false |
| 693 | } |
| 694 | |
| 695 | end := -1 |
| 696 | for i := 1; i < len(lines); i++ { |
| 697 | if strings.TrimSpace(lines[i]) == fence { |
| 698 | end = i |
| 699 | break |
| 700 | } |
| 701 | } |
| 702 | if end == -1 { |
| 703 | return "", data, false |
| 704 | } |
| 705 | |
| 706 | for _, line := range lines[1:end] { |
| 707 | if m := titleRe.FindStringSubmatch(line); len(m) == 2 { |
| 708 | title = trimQuotes(strings.TrimSpace(m[1])) |
| 709 | break |
| 710 | } |
| 711 | } |
| 712 | return title, strings.Join(lines[end+1:], "\n"), true |
| 713 | } |
| 714 | |
| 715 | // normalizeDocsFlattenMarkdown unifies line endings and collapses runs of |
| 716 | // blank lines to a single one. |