summaryrefslogtreecommitdiffstats
path: root/utils/markdown/blocks.go
blob: 14972f943e694d6dc631dfda20384024762773eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package markdown

import (
	"strings"
)

type continuation struct {
	Indentation int
	Remaining   Range
}

type Block interface {
	Continuation(indentation int, r Range) *continuation
	AddLine(indentation int, r Range) bool
	Close()
	AllowsBlockStarts() bool
	HasTrailingBlankLine() bool
}

type blockBase struct{}

func (*blockBase) AddLine(indentation int, r Range) bool { return false }
func (*blockBase) Close()                                {}
func (*blockBase) AllowsBlockStarts() bool               { return true }
func (*blockBase) HasTrailingBlankLine() bool            { return false }

type ContainerBlock interface {
	Block
	AddChild(openBlocks []Block) []Block
}

type Range struct {
	Position int
	End      int
}

func closeBlocks(blocks []Block, referenceDefinitions *[]*ReferenceDefinition) {
	for _, block := range blocks {
		block.Close()
		if p, ok := block.(*Paragraph); ok && len(p.ReferenceDefinitions) > 0 {
			*referenceDefinitions = append(*referenceDefinitions, p.ReferenceDefinitions...)
		}
	}
}

func ParseBlocks(markdown string, lines []Line) (*Document, []*ReferenceDefinition) {
	document := &Document{}
	var referenceDefinitions []*ReferenceDefinition

	openBlocks := []Block{document}

	for _, line := range lines {
		r := line.Range
		lastMatchIndex := 0

		indentation, indentationBytes := countIndentation(markdown, r)
		r = Range{r.Position + indentationBytes, r.End}

		for i, block := range openBlocks {
			if continuation := block.Continuation(indentation, r); continuation != nil {
				indentation = continuation.Indentation
				r = continuation.Remaining
				additionalIndentation, additionalIndentationBytes := countIndentation(markdown, r)
				r = Range{r.Position + additionalIndentationBytes, r.End}
				indentation += additionalIndentation
				lastMatchIndex = i
			} else {
				break
			}
		}

		if openBlocks[lastMatchIndex].AllowsBlockStarts() {
			if newBlocks := blockStart(markdown, indentation, r, openBlocks[:lastMatchIndex+1], openBlocks[lastMatchIndex+1:]); newBlocks != nil {
				didAdd := false
				for i := lastMatchIndex; i >= 0; i-- {
					if container, ok := openBlocks[i].(ContainerBlock); ok {
						if newBlocks := container.AddChild(newBlocks); newBlocks != nil {
							closeBlocks(openBlocks[i+1:], &referenceDefinitions)
							openBlocks = openBlocks[:i+1]
							openBlocks = append(openBlocks, newBlocks...)
							didAdd = true
							break
						}
					}
				}
				if didAdd {
					continue
				}
			}
		}

		isBlank := strings.TrimSpace(markdown[r.Position:r.End]) == ""
		if paragraph, ok := openBlocks[len(openBlocks)-1].(*Paragraph); ok && !isBlank {
			paragraph.Text = append(paragraph.Text, r)
			continue
		}

		closeBlocks(openBlocks[lastMatchIndex+1:], &referenceDefinitions)
		openBlocks = openBlocks[:lastMatchIndex+1]

		if openBlocks[lastMatchIndex].AddLine(indentation, r) {
			continue
		}

		if paragraph := newParagraph(markdown, r); paragraph != nil {
			for i := lastMatchIndex; i >= 0; i-- {
				if container, ok := openBlocks[i].(ContainerBlock); ok {
					if newBlocks := container.AddChild([]Block{paragraph}); newBlocks != nil {
						closeBlocks(openBlocks[i+1:], &referenceDefinitions)
						openBlocks = openBlocks[:i+1]
						openBlocks = append(openBlocks, newBlocks...)
						break
					}
				}
			}
		}
	}

	closeBlocks(openBlocks, &referenceDefinitions)

	return document, referenceDefinitions
}

func blockStart(markdown string, indentation int, r Range, matchedBlocks, unmatchedBlocks []Block) []Block {
	if r.Position >= r.End {
		return nil
	}

	if start := blockQuoteStart(markdown, indentation, r, matchedBlocks, unmatchedBlocks); start != nil {
		return start
	} else if start := listStart(markdown, indentation, r, matchedBlocks, unmatchedBlocks); start != nil {
		return start
	} else if start := indentedCodeStart(markdown, indentation, r, matchedBlocks, unmatchedBlocks); start != nil {
		return start
	} else if start := fencedCodeStart(markdown, indentation, r, matchedBlocks, unmatchedBlocks); start != nil {
		return start
	}

	return nil
}

func blockStartOrParagraph(markdown string, indentation int, r Range, matchedBlocks, unmatchedBlocks []Block) []Block {
	if start := blockStart(markdown, indentation, r, matchedBlocks, unmatchedBlocks); start != nil {
		return start
	}
	if paragraph := newParagraph(markdown, r); paragraph != nil {
		return []Block{paragraph}
	}
	return nil
}