Coder Social home page Coder Social logo

embedding local image about clerk HOT 6 CLOSED

nextjournal avatar nextjournal commented on May 17, 2024
embedding local image

from clerk.

Comments (6)

genmeblog avatar genmeblog commented on May 17, 2024 2

Let us know when embedding files will be added natively.

from clerk.

mk avatar mk commented on May 17, 2024 1

It includes the image as base64 encoded inside the html, which has pros and cons.

This is currently only happening when generating a static html, when Clerk is running it's served via another request. We'll consider a knob in the future to allow publish images as files in the future.

from clerk.

mk avatar mk commented on May 17, 2024

This works right now but we want to make this easier, probably even include default viewers so this works out of the box.

;; # 🏞 Customizing Fetch
;; Showing how to use a custom `fetch-fn` with a `content-type` to let Clerk serve arbitrary things, in this case a PNG image.
(ns ^:nextjournal.clerk/no-cache image
  (:require [nextjournal.clerk :as clerk]
            [babashka.fs :as fs])
  (:import (java.nio.file Path Files)))


;; We set a custom viewer for `java.nio.file.Path` that includes a `:fetch-fn`, returning a wrapped value with a `:nextjournal/content-type` key set.
(clerk/set-viewers! [{:pred #(instance? Path %)
                      :fetch-fn (fn [_ path] {:nextjournal/content-type "image/png"
                                              :nextjournal/value (Files/readAllBytes path)})
                      :render-fn (fn [blob] (v/html [:img {:src (v/url-for blob)}]))}])

(fs/path "/Users/mk/dev/clerk-demo/datasets/james_clerk_maxwell.png")

CleanShot 2021-12-17 at 11 56 16@2x

from clerk.

behrica avatar behrica commented on May 17, 2024

I have tried this as well for dynamically generated images.
It is working as well for that case.

It includes the image as base64 encoded inside the html, which has pros and cons.
I am bit concerned on performance on this, but have not data so far

from clerk.

genmeblog avatar genmeblog commented on May 17, 2024

Thanks!

base64 encoding will blow the size of html for more than several images. base64 adds 25% of original size of the image.

Question about above example, what about support for more than one image type (say: png, jpg and gif)?
Also: does it work for relative paths and after building static pages? I want to push html to github and be sure that images still will be visible.

(haven't tried it yet)

from clerk.

genmeblog avatar genmeblog commented on May 17, 2024

Ok, look like it works well and images are embedded inside static html when building is done. Maybe consider copying files?
Multiple mime types can be recognized from a path and extension (and should be bundled to cover most popular file types).

Anyway it works well without too much hassle. Thanks a lot!

from clerk.

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.