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

Provide custom image adapter #269

Closed

Conversation

lucperkins
Copy link
Contributor

Fixes #268. Analogous to the work in PR #266.

I've marked this as a draft because I haven't quite gotten to work. The issue is that I'm not sure how to ignore the title, i.e. ![This is the title](/some/url.png). Any pointers here would be greatly appreciated.

@kivikakk kivikakk force-pushed the custom-image-adapter branch from 1573a33 to 23ea2b1 Compare March 31, 2023 04:41
@kivikakk kivikakk changed the base branch from main to xml-sourcepos March 31, 2023 04:41
@kivikakk kivikakk force-pushed the custom-image-adapter branch from 23ea2b1 to e029c63 Compare March 31, 2023 04:43
@kivikakk
Copy link
Owner

Hey! Sorry I didn't get back to you earlier. Life's been a bit.

Comrak has had some fairly major changes since, so I rebased your work, and made some minor corrections to suit the new way of doing adapters. We're now back to where you were with the caption.

Actually, the problem is kind of opposite to what you might expect. That's not the image title you're seeing -- that's the image alt. Those are separate in CommonMark; see § 6.4 Images.

Using what I've currently got pushed, here's the output:

<p data-sourcepos="1:1-1:35"><figure data-sourcepos="1:1-1:35"><a href="/img/logo.png" target="_blank"><img src="/img/logo.png"></a></figure>Here is a caption</p>

The alt is just shown afterward, because the AST actually looks like this here:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document SYSTEM "CommonMark.dtd">
<document sourcepos="1:1-1:35" xmlns="http://commonmark.org/xml/1.0">
  <paragraph sourcepos="1:1-1:35">
    <image sourcepos="1:1-1:35" destination="/img/logo.png" title="">
      <text sourcepos="1:3-1:19" xml:space="preserve">Here is a caption</text>
    </image>
  </paragraph>
</document>

Alt text is nested inside the image as a text node. Note the empty title="" attribute -- you get that by specifying it after the URL like this: ![alt](/url "title").

Assuming you actually want to use the alt text instead for the caption -- since it's a lot more convenient for the user -- you'll instead want to use html's collect_text to aggregate text from nested nodes (see NodeValue::Heading's case for how it does that), and then pass that in the ImageMeta. I suggest naming the attribute caption instead of title, perhaps, just so it's clear it's different.

You'll also want to then drop all the children nodes before finishing, otherwise they'll still get rendered afterward.

Let me know if you have any other questions!

@kivikakk
Copy link
Owner

Alternatively, you could use the same return Ok(true); trick that the default image renderer itself does, after checking that there are indeed any child nodes to render as <figcaption>.

@kivikakk kivikakk deleted the branch kivikakk:xml-sourcepos March 31, 2023 07:51
@kivikakk kivikakk closed this Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide custom image adapter
2 participants