Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getLineOffset() to MarkdownInput #1039

Open
bwl21 opened this issue Aug 2, 2024 · 1 comment
Open

add getLineOffset() to MarkdownInput #1039

bwl21 opened this issue Aug 2, 2024 · 1 comment
Labels
enhancement New functionality or behavior
Milestone

Comments

@bwl21
Copy link

bwl21 commented Aug 2, 2024

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 bwl21 added the enhancement New functionality or behavior label Aug 2, 2024
@bwl21 bwl21 changed the title add ->getLineOffset() to MarkdownInput add getLineOffset() to MarkdownInput Aug 2, 2024
@colinodell
Copy link
Member

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 colinodell added this to the v3.0 milestone Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New functionality or behavior
Projects
None yet
Development

No branches or pull requests

2 participants