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

Support inline option on SVG images #453

Closed
mariotoffia opened this issue Aug 31, 2021 · 7 comments · Fixed by #472
Closed

Support inline option on SVG images #453

mariotoffia opened this issue Aug 31, 2021 · 7 comments · Fixed by #472

Comments

@mariotoffia
Copy link

When rendering asciidoc to plain html I can do e.g. image::my-image.svg[opts=inline] and it would inline the image into the resulting html.

Is is possible to do so with asciidoctor-reveal.js.

Cheers,
Mario :)

@rezib
Copy link

rezib commented May 9, 2022

I guess it misses conditions in this template to support this feature:

- if attr? :link
a.image href=(attr :link) target=(attr :window) data-preview-link=(bool_data_attr :link_preview)
img src=image_uri(attr :target) alt=(attr :alt) width=(width) height=(height) style=((attr? :background) ? "background: #{attr :background}" : nil)
- else
img src=image_uri(attr :target) alt=(attr :alt) width=(width) height=(height) style=((attr? :background) ? "background: #{attr :background}" : nil)

For the sake of justifying the need behind this issue, with inline SVG it is possible to interact with the content of the SVG (eg. links, animation, scripting, etc). It is actually well explained in asciidoctor documentation.

This feature would open new possibilities for even better presentations with asciidoctor-reveal.js 😀

@kxygk
Copy link

kxygk commented May 30, 2022

Thanks for the issue. I was very confused I'd done something wrong.
If this could further be defined globally for a document - that'd be quite handy

@mariotoffia
Copy link
Author

@rezib

This feature would open new possibilities for even better presentations with asciidoctor-reveal.js

I totally agree, I think this is the mosst hurdle since I often mail my presentations - now I need to publish them all the time.

Cheers,
Mario :)

@Riduidel
Copy link

Riduidel commented Jun 2, 2022

If someone stil encounters the issue, there is a possible hack done the following way

  1. Add the role=svg to each generated svg diagram. This will add the svg html class to the image container div (well, provided you use a block image, but I guess you did)
  2. Create a footer docinfo file
  3. In that footer docinfo file, add the following javascript
<script>
function replaceSVGImagesTags() {
	// We add the svg role in asciidoc document to SVG images.
	// This is in fact added to container div.
	// So we locate those container div contained images to replace them
	// Hopefully modern JS has a pseudo-CSS syntax for that
	const svgImages = document.querySelectorAll("div.svg img")
	// Now let's browse those images and change their content
	svgImages.forEach(img => {
		const src = img.getAttribute("src")
		console.info("replacing image having source ", src)
		const parent = img.parentElement
		parent.removeChild(img)
		// Now create an object which data is the given src
		fetch(src)
	        .then(response => response.text())
	        .then(responseText => new window.DOMParser().parseFromString(responseText, "text/xml"))
	        .then(responseXML => parent.appendChild(responseXML.rootElement));
	})
}

replaceSVGImagesTags()
</script>
  1. In your presentation stylesheet, don't forget to add div.svg { font-size: initial } otherwise your font will render weirdly.

With these modifications, your image diagrams will all be inlined as direct SVG tags. This allow more interactivity, and ability to select individual SVG elements

@ggrossetie ggrossetie changed the title Q: Is it possible to do opts=inline for a svg image inclusion (as with html renderer) Support inline option on SVG images Jun 3, 2022
@ggrossetie
Copy link
Member

The relevant code in the built-in HTML5 converter: https://github.com/asciidoctor/asciidoctor/blob/a040b60300efaa0d75cc6a3b031241dbe7b5a864/lib/asciidoctor/converter/html5.rb#L1214-L1220

It seems that we can reuse read_svg_contents .

@mojavelinux
Copy link
Member

Let's do it. If you need me to lend a hand, just call on me.

ggrossetie added a commit to ggrossetie/asciidoctor-reveal.js that referenced this issue Jun 6, 2022
@barthel
Copy link

barthel commented Mar 30, 2023

@obilodeau @ggrossetie ping. Please merge the PR if u agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants