Git Product home page Git Product logo

bridgetown-svg-inliner's Introduction

Bridgetown SVG Inliner

Tests Gem Version

A Bridgetown plugin that provides a liquid tag and ERB helper to inline SVG files within the HTML markup.

Installation

Run this command to add this plugin to your site's Gemfile:

$ bundle add "bridgetown-svg-inliner"

Initialize it in your config/initializers.rb file.

Bridgetown.configure do |config|
  # ...

  init :"bridgetown-svg-inliner"
end

Usage

This plugin provides an svg tag in Liquid and an svg helper in ERB or other Tilt based templating langugages.

<!-- Liquid -->
{% svg "/assets/icons/thumbs-up.svg" %}
<!-- ERB -->
<%= svg "/assets/icons/thumbs-up.svg" %>
<!-- Serbea -->
{%= svg "/assets/icons/thumbs-up.svg" %}

Attributes

You can pass in attributes that you'd like to include on the svg tag in the HTML output.

<!-- Liquid -->
{% svg "/assets/icons/thumbs-up.svg", class: "icon" %}
<!-- ERB -->
<%= svg "/assets/icons/thumbs-up.svg", class: "icon" %>
<!-- Serbea -->
{%= svg "/assets/icons/thumbs-up.svg", class: "icon" %}
<!-- Output -->
<svg class="icon">...</svg>

Variables

Liquid variables

You can use Liquid variables by enclosing them in double braces ({{ }})

<!-- Liquid -->
{% assign svg_file = "thumbs-up" %}
{% assign svg_class_list = "icon icon--small" %}
{% svg "/assets/icons/{{ svg_file }}.svg", class: "{{ svg_class_list }}" %}

ERB variables

You can use ERB variables.

<% svg_file = "thumbs-up.svg" %>
<% svg_class_list = "icon icon--small" %>
<%= svg "/assets/icons/" + svg_file, class: "svg_class_list" %>

Serbea variables

You can use Serbea variables.

{% svg_file = "thumbs-up.svg" %}
{% svg_class_list = "icon icon--small" %}
{%= svg "/assets/icons/" + svg_file, class: "svg_class_list" %}

Testing

  • Run bundle exec rake test to run the test suite
  • Or run script/cibuild to validate with Rubocop and Minitest together.

Contributing

  1. Fork it (https://github.com/ayushn21/bridgetown-svg-inliner/fork)
  2. Clone the fork using git clone to your local development machine.
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request

License

Bridgetown SVG Inliner is released under the MIT License.

Copyright © 2023 Ayush Newatia

bridgetown-svg-inliner's People

Contributors

ayushn21 avatar ikass avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

topofocus ikass

bridgetown-svg-inliner's Issues

Add ability to specify default directory and omit .svg extension

It would be nice to be able to specify a default subdirectory like images that automatically gets prepended to the path we pass into svg while also omitting the file extension as it seems very redundant given the nature of the method.

Example:

A file existing at src/images/icons/check.svg would allow us to simply write erb <%= svg 'icons/check' %> instead of erb <%= svg 'images/icons/check.svg' %>

Bridgetownrb returns an error when using {% svg "path" %} in a code piece

As I said in my blog I wanted to show off how to install and use this gem in Bridgetown

when I was writing in a markdown file and put the following lines as code blocks, the compiler just breaks down.

<!-- Liquid -->
{% svg "/assets/icons/thumbs-up.svg" %}
<!-- ERB -->
<%= svg "/assets/icons/thumbs-up.svg" %>

in order to solve the problem, I put a # to scape the code.

<!-- Liquid -->
{#% svg "/assets/icons/thumbs-up.svg" %}
<!-- ERB -->
<#%= svg "/assets/icons/thumbs-up.svg" %>

reference article

Support for Bridgetown v1.2.0

When adding this gem to a Bridgetown v1.2.0 project I get a deprecation warning in the console.

[Bridgetown]       Initializing: The `bridgetown-svg-inliner' initializer could not be found

Relevant code:

# Gemfile
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem "bridgetown", "1.2.0.beta4"

gem "bridgetown-svg-inliner"

group :development do
  gem "puma", "~> 5.6"
end
# config/initializers.rb
Bridgetown.configure do |config|
  permalink "pretty"
  template_engine "erb"

  init :"bridgetown-svg-inliner"
end

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.