func generateAnchorIds()

in docgen/lib/render/render.go [41:77]


func generateAnchorIds(doc *proto.Document) *proto.Document {
	// Maps from a base anchor ID to the number of anchors with the same base ID.
	// Anchors sharing a base ID will differ only in the suffixes, e.g. "-1".
	anchorCounts := make(map[string]int)
	for _, id := range predefinedAnchorIds {
		anchorCounts[id] = 1
	}

	result := &proto.Document{}
	*result = *doc
	for _, taskGroup := range result.TaskGroups {
		if len(taskGroup.AnchorId) > 0 {
			// Handles a user-defined anchor ID. This ID must be unique.
			if _, found := anchorCounts[taskGroup.AnchorId]; found {
				panic(fmt.Sprintf("Duplicate anchor: %s", taskGroup.AnchorId))
			}
		} else {
			// Generates an anchor ID from the title.
			desiredId := makeAnchorId(taskGroup.Title)
			taskGroup.AnchorId = updateAnchorId(anchorCounts, desiredId)
		}

		for _, task := range taskGroup.Tasks {
			if len(task.AnchorId) > 0 {
				// Handles a user-defined anchor ID. This ID must be unique.
				if _, found := anchorCounts[task.AnchorId]; found {
					panic(fmt.Sprintf("Duplicate anchor: %s", task.AnchorId))
				}
			} else {
				// Generates an anchor ID from the title.
				desiredId := makeAnchorId(task.Title)
				task.AnchorId = updateAnchorId(anchorCounts, desiredId)
			}
		}
	}
	return result
}