Git Product home page Git Product logo

Comments (4)

wolfspyre avatar wolfspyre commented on June 3, 2024

Hi!

how are you asking hugo to generate this? there’s a few ways to render an svg… theres a lot of nuance, as most of the tooling around images, especially in hugo have an expectation of a raster image… and don’t play well with vectors… so there’s some nuance and odd contortions one has to do at times to get things to play nice together

if you can share the source you’re working with we might be able to offer some suggestions as to a solution that’ll work

from congo.

stereobooster avatar stereobooster commented on June 3, 2024

Here is code responsible for this

{{ range (findRESubmatch `<svg[^>]*width=["']([.0-9]*)["'a-zA-Z]` $svgContent 1) }}
{{ $width = index . 1 }}
{{ end }}
{{ range (findRESubmatch `<svg[^>]*height=["']([.0-9]*)["'a-zA-Z]` $svgContent 1) }}
{{ $height = index . 1 }}
{{ end }}
{{ if (eq "" $width $height) }}
{{ range (findRESubmatch `<svg[^>]*viewBox=["']?([.0-9]*) ([.0-9]*) ([.0-9]*) ([.0-9]*)` $svgContent 1) }}
{{ $width = index . 3 }}
{{ $height = index . 4 }}
{{ end }}

options are:

  • change regexp to only work if there are no units or units are explicitly pixels
  • implement units conversion, but I think cm depends on the density of pixels on device and there is no universal way to do this "on the server"

UPD solution is to capture width and height together with units and output them in html as is.

from congo.

jpanther avatar jpanther commented on June 3, 2024

UPD solution is to capture width and height together with units and output them in html as is.

@stereobooster - I don't think it's as simple as that as the img tag only accepts dimensions in pixels. Updating the capture group to pick up the units still doesn't get the browser to display it at the correct size.

Using the reference image provided above and this modification:

    {{ range (findRESubmatch `<svg[^>]*width=["']([.0-9]*[a-zA-Z]*)["']` $svgContent 1) }}
      {{ $width = index . 1 }}
    {{ end }}
    {{ range (findRESubmatch `<svg[^>]*height=["']([.0-9]*[a-zA-Z]*)["']` $svgContent 1) }}
      {{ $height = index . 1 }}
    {{ end }}

Gives the following output:

<img src="test.svg" width="3.07cm" height="3.07cm" class="mx-auto my-0 rounded-md" alt="Test" ...>

But the actual image is still displayed as if it was 3.07px x 3.07px.

from congo.

stereobooster avatar stereobooster commented on June 3, 2024

@jpanther Indeed. I didn't realized that HTML width is not the same as CSS width.

Hm... Maybe we can use CSS (style="{width:3.07cm, height:3.07cm}")? Maybe aspect-ratio

Other options:

  • do unit conversion (as proposed above)
  • only support cases when there are no units or units are px. If there are unsupported units do not output width and height in HTML (so browser would detect width and height the old way)
    • height=["']([.0-9]*)(px)?["']

from congo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.