1
import { Node, TemplateChildNode } from '@vue/compiler-dom'
2 1
import { isCommentNode } from './guards'
3

4
/**
5
 * Extract leading comments to an html node
6
 * Even if the comment is on multiple lines it's still taken as a whole
7
 * @param templateAst
8
 * @param rootLeadingComment
9
 */
10 1
export default function extractLeadingComment(
11
	siblings: TemplateChildNode[],
12 1
	templateAst: Node
13
): string[] {
14
	// if the slot has no comment siblings, the slot is not documented
15 1
	if (siblings.length < 1) {
16 1
		return []
17
	}
18
	// First find the position of the slot in the list
19 1
	let i = siblings.length - 1
20 1
	let currentSlotIndex = -1
21 1
	do {
22 1
		if (siblings[i] === templateAst) {
23 1
			currentSlotIndex = i
24
		}
25 1
	} while (currentSlotIndex < 0 && i--)
26

27
	// Find the first leading comment
28
	// get all siblings before the current node
29 1
	const slotSiblingsBeforeSlot = siblings.slice(0, currentSlotIndex).reverse()
30

31
	// find the first node that is not a potential comment
32 1
	const indexLastComment = slotSiblingsBeforeSlot.findIndex(sibling => !isCommentNode(sibling))
33

34
	// cut the comments array on this index
35 1
	const slotLeadingComments = (indexLastComment > 0
36 1
		? slotSiblingsBeforeSlot.slice(0, indexLastComment)
37 1
		: slotSiblingsBeforeSlot
38
	)
39
		.reverse()
40
		.filter(isCommentNode)
41

42
	// return each comment text
43 1
	return slotLeadingComments.map(comment => comment.content.trim())
44
}

Read our documentation on viewing source code .

Loading