package org.codetome.snap.adapter.parser

import com.vladsch.flexmark.ast.Heading
import com.vladsch.flexmark.ast.LinkRef
import com.vladsch.flexmark.ast.Node
import org.codetome.snap.model.ParseContext
import org.codetome.snap.service.parser.Header
import org.codetome.snap.service.parser.HeaderSegmentParser

class MarkdownHeaderSegmentParser : HeaderSegmentParser<Node>() {

    override fun parseSegment(nodes: List<Node>, context: ParseContext): Pair<Header, List<Node>> {
        val rootNode = nodes.first() as Heading
        val text = rootNode.firstChild.chars.trim().toString()
        val linkRef = rootNode.firstChild.next as LinkRef
        val linkContent = linkRef.reference.trim().toString()
        return Pair(
                Header(text, linkContent),
                nodes.drop(1))
    }
}

