Giter VIP home page Giter VIP logo

code-tag's Introduction

code-tag npm version (size)

noop functions to help formatters and syntax highlighters recognize embedded code

When embedding other languages in JavaScript, you can mark those strings with a tag function to help JavaScript tools recognize the string as code:

document.body.innerHTML = html`
	<p>This is inline HTML</p>
	<!-- Including comments -->
	<style>.and {css: 'too'}</style>
`;

You can find such tag functions in:

  • code-tag: this package, it returns the string as is
  • lit-html: it helps write Web Components
  • Apollo: it parses GraphQL strings
  • Emotion: it defines CSS-in-JS
  • etc…

Here are some tools that support them natively:

  • Prettier: it formats the strings as real non-JavaScript code
  • GitHub: it highlights the syntax in the strings as code (as seen in the example above)

Install

npm install code-tag

Usage

import {html, css, gql, md, sql} from 'code-tag';
// Or:
// const {html, css, gql, md, sql} = require('code-tag');

document.body.innerHTML = html`
	<p>This is HTML in JS</p>
`;

document.querySelector('style').textContent = css`
	.this.is {
		css: 'in JS';
	}
`;

await githubQuery(gql`
	query {
		repository(owner: "fregante", name: "template-tags") {
			nameWithOwner
		}
	}
`);

yourMarkdownConverter(md`
	# Markdown

	Is _highlighted_ [as well](https://www.youtube.com/watch?v=dQw4w9WgXcQ)
`);

await sqlQuery(sql`select * from users`);

There's also an any export that you can rename as you please:

import {any as mdx} from 'code-tag';

mdx`
	Some other <New>Language</New>
`;

License

MIT © Federico Brigante

code-tag's People

Contributors

fregante avatar vadimdemedes avatar fisker avatar

Stargazers

Joshua Jacobs 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.