How to get the line number where link reference definition is defined? #1036
Unanswered
LastDragon-ru
asked this question in
Q&A
Replies: 1 comment
-
|
I had an issue with reference links which were part of the Google Docs markdown export. The HTML output kept them as seperate elements, leaving the original markdown reference intact, and converting the link to a standardanchor href. I'd like to know if I've missed something in the documentation about how to handle this. What I've ended up doing was creating a wrapper function to convert the reference links to individual anchors before processing the markdown. Might need some testing: /**
* Process a markdown string with link references.
*
* @param string $markdown
* The markdown string to process.
*
* @return string
* A string containing the processed HTML.
*/
public function processMarkdown(string $markdown): string {
// Find all reference-style image definitions (e.g., [image1]: <data:...>).
preg_match_all('/^\[([^\]]+)\]:\s*<([^>]+)>/m', $markdown, $matches, PREG_SET_ORDER);
$image_references = [];
foreach ($matches as $match) {
$id = $match[1];
$url = $match[2];
$image_references[$id] = $url;
// Remove the definition line from the markdown.
$markdown = str_replace($match[0], '', $markdown);
}
// Replace reference-style image links with inline-style links.
// e.g., ![Alt text][image1] becomes .
$markdown = preg_replace_callback(
'/!\[([^\]]*)\]\[([^\]]+)\]/',
function ($link_match) use ($image_references) {
$alt_text = $link_match[1];
$id = $link_match[2];
return $image_references[$id] ? "" : $link_match[0];
},
$markdown
);
$converter = new CommonMarkConverter();
return $converter->convert($markdown)->getContent();
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I want to update link references in markdown, until #419 is done, the only way is to replace it in the text. But I cannot find a way how to determine the line where reference is defined :(
ReferenceInterfacedoesn't hold this information at allFor example, for this document the line is
3.Beta Was this translation helpful? Give feedback.
All reactions