func separatedForTermDefinition()

in Sources/SwiftDocC/Model/Rendering/Content/Extensions/RenderTermLists.swift [129:191]


    func separatedForTermDefinition(separator: String) -> (termInlines: [RenderInlineContent], definitionInlines: [RenderInlineContent])? {
        let termKeyword = "term "
        
        // Make sure this collection of inline contents starts with the
        // term keyword, ignoring any extra whitespace before the keyword
        guard case let .text(text) = first, text.lowercased().removingLeadingWhitespace().hasPrefix(termKeyword) else {
            return nil
        }
        
        var termInlines = [RenderInlineContent]()
        var definitionInlines = [RenderInlineContent]()
        var foundSeparator = false
        
        for inline in self {
            if foundSeparator {
                // All content after the separator should be considered
                // part of the definition
                definitionInlines.append(inline)
            } else if let (termInline, definitionInline) = inline.separatedForTermDefinition(separator: separator) {
                // Only accept the returned term and definition inline elements
                // if they are not empty
                if termInline != nil {
                    termInlines.append(termInline!)
                }
                if definitionInline != nil {
                    definitionInlines.append(definitionInline!)
                }
                foundSeparator = true
            } else {
                // All content before the separator and not including the
                // separator should be part of the term
                termInlines.append(inline)
            }
        }
        
        guard foundSeparator else {
            // Term indicators weren't found
            return nil
        }
        
        // Remove the keyword from the term inlines and drop the first inline if
        // removing the keyword produced an empty inline in its place
        let termInlinesKeywordRemoved: [RenderInlineContent]
        if let firstInlineRemovingKeyword = termInlines.first?.removingTermKeyword(termKeyword) {
            termInlinesKeywordRemoved = [firstInlineRemovingKeyword] + termInlines.dropFirst()
        } else {
            if termInlines.count == 1 {
                // Don't allow a term with no contents to have an empty
                // array of inline elements
                termInlinesKeywordRemoved = [RenderInlineContent.text("")]
            } else {
                termInlinesKeywordRemoved = Array(termInlines.dropFirst())
            }
        }
        
        if definitionInlines.isEmpty {
            // Don't allow a definition with no contents to have an empty
            // array of inline elements
            definitionInlines = [RenderInlineContent.text("")]
        }
        
        return (termInlines: termInlinesKeywordRemoved, definitionInlines: definitionInlines)
    }