Skip to content

kawarimidoll/deno-markup-tag

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

deno-markup-tag

ci deno version LICENSE deno.land nest badge

Usage

deno doc

Import

From deno.land/x:

import { tag } from "https://deno.land/x/[email protected]/mod.ts";

From x.nest.land:

import { tag } from "https://x.nest.land/[email protected]/mod.ts";

From pax.deno.dev:

import { tag } from "https://pax.deno.dev/kawarimidoll/[email protected]";

tag

Render markup tag.

// common usage
assertEquals(
  tag("div", { id: "foo", class: "bar" }, "Hello world!"),
  `<div id="foo" class="bar">Hello world!</div>`,
);

// void (no-close) tag
assertEquals(tag("meta", { charset: "utf-8" }), `<meta charset="utf-8">`);

// nested tags
assertEquals(
  tag("ul", { class: "nav" }, tag("li", "first"), tag("li", "second")),
  `<ul class="nav"><li>first</li><li>second</li></ul>`,
);

// boolean attributes
assertEquals(
  tag("button", { type: "button", disabled: true }, "disabled"),
  `<button type="button" disabled>disabled</button>`,
);

// skip attributes
assertEquals(
  tag("input", { type: "text", readonly: false }),
  `<input type="text">`,
);

Character references

These constants are exported.

const NBSP = "&nbsp;";
const LT = "&lt;";
const GT = "&gt;";
const AMP = "&amp;";
const QUOT = "&quot;";

sanitize

Sanitize &, <, > and " in string.

// common usage
assertEquals(
  sanitize(`<img src="https://www.example.com?width=10&height=10">`),
  "&lt;img src=&quot;https://www.example.com?width=10&amp;height=10&quot;&gt;",
);

// ignore sanitizing specific characters
assertEquals(sanitize("<br>", { lt: false, gt: false }), "<br>");