commonmark icon indicating copy to clipboard operation
commonmark copied to clipboard

add getLineOffset() to MarkdownInput

Open bwl21 opened this issue 1 year ago • 1 comments

Description

I have a markdown File with frontMatter, so I use the frontmatterextension to get the source witthout frontmatter. But then the line numbers in the nodes no longer match with the original source file.

In order to fix this, I should have access to the lineOffset which gives the first line of the source without frontmatter.

By this I could implement a proper reporting.

As of now I work around this. But it would be simpler if MarkdownInput had a getter for $lineOffset

maybe it would even be possible that the frontmatter is represented in the AST.

Example

    public function load(string $markdown): self {
        $frontMatterAndMarkdown = $this->parseFrontMatter($markdown);
        $this->sourceText = new SourceText($frontMatterAndMarkdown->markdown);
        $this->documentAst = $this->markdownParser->parse($frontMatterAndMarkdown->markdown);
        $this->lineOffset = $this->getLineOffset($markdown, $frontMatterAndMarkdown->markdown);
        return $this;
    }

    private function getLineOffset($markdownWithFrontmatter, $markdown) {
        $startpos = strpos($markdownWithFrontmatter, $markdown);
        return substr_count($markdownWithFrontmatter, "\n", 0, $startpos);
    }

would be

    public function load(string $markdown): self {
        $frontMatterAndMarkdown = $this->parseFrontMatter($markdown);
        $this->sourceText = new SourceText($frontMatterAndMarkdown->markdown);
        $this->documentAst = $this->markdownParser->parse($frontMatterAndMarkdown->markdown);
        $this->lineOffset = $frontMatterAndMarkdown->getLineOffset(); // <---
        return $this;
    }

Did this project help you today? Did it make you happy in any way?

This project is great, has a clear architecture and no surprises. Thanks so much.

bwl21 avatar Aug 02 '24 09:08 bwl21

Thanks for the suggestion!

I'm not very happy with the current front matter implementation. Not just because of the line numbering issue, but also how we use RenderedContentWithFrontMatter to expose it.

I really like the idea of representing the front matter in the AST! We'd need that to support features like #419 anyway.

colinodell avatar Dec 10 '24 13:12 colinodell