GithubHelp home page GithubHelp logo

f1lt3r / implant Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 87 KB

๐ŸŒฑ asynchronous inline content replacement

Home Page: http://f1lt3r.io

License: MIT License

JavaScript 100.00%
implant insert replace content string async promise object handler dynamic

implant's Introduction

Implant

๐ŸŒฑ asynchronous inline content replacement

Build Status Coverage Status NPM Version XO code style

Support the development of Implant.

Install

$ yarn add implant

Fetching Network Resources

Reference an Implant handler from your HTML:

<p>{get: "https://f1lt3r.github.io/foobar/bazqux.html"}</p>

Write the Implant handler:

const request = require('request')

const handlers = {
    get: url => new Promise((resolve, reject) => {
        request(url, (err, res, body) => {
            if (err) {
                return reject(err)
            }

            resolve(body)
        })
    })
}

const result = implant(html, handlers)'

The content of bazqux.html is fetched from the web when implant handler is executed.

Checkout the result:

<p><h1>Hello, wombat!</h1></p>

Using JavaScript Objects

You can use plain JavaScript object within your HTML:

<div>
    <!-- Implant reference object -->
    {article: {
        id: 8421,
        section: 3
    }}
</div>

Your Implant handler can reference data using the properties you pass:

// Some store of data
const articles = {
    8421: {
        sections: {
            3: 'Foo. Or foo not. There is no bar.'
        }
    }
}

// Implant handler returns data from store
const handlers = {
    article: ref => {
        const {id, section} = ref
        return articles[id].sections[section]
    }
}

const result = implant(html, handlers)

Result:

<div>Foo. Or foo not. There is no bar.</div>

Using Illegal JavaScript Values

It is also possible to use illegal JavaScript values, such as references to objects that do not exist. For example:

<div>{foo: i.find.your.lack.of.qux.disturbing}</div>

When an illegal value is encountered, Implant pass back a stringified version of the handler.

const handlers = {
    foo: uri => console.log
    // 'i.find.your.lack.of.qux.disturbing'
}

Handling values this way allows you to write cleaner syntax in your content templates by excluding quotes; or designing your own custom syntax.

You might use this feature to reference filenames without quotes:

<div>{article: programming-101.md}</div>

Then you could fetch and render the article like this.

const handlers = {
    foo: uri => fetchPost(uri)
}

Recusion

You can recurse through the result of you implant like this:

const html = {
    level1: '<div>1 {include: level2}</div>',
    level2: '<div>2 {include: level3}</div>',
    level3: '<div>3 {include: level4}</div>'
}

const handlers = {
    include: ref => {
        return html[ref]
    }
}

const opts = {
    maxRecursion: 3
}

;(async () => {
    const result = await implant(html.level1, handlers, opts)

})()

Result:

<div>1 <div>2 <div>3 {include: level4}</div></div></div>

Why Recusion?

You may want to use recursion if you are building a just-in-time dependancy tree. For example: if your implant fetched a dependency that contained a reference to another depdendancy.

Note: if you do not specify the maxRecusion option, implant will only run once.

Credits

Thanks to the following designers from the Noun Project for the vectors used in the lead graphic.

implant's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.