Guide

Getting started

Install @samline/string with a package manager or a script tag and convert your first text in the browser or on the server.

Install @samline/string, then follow the Package / ESM or the HTML / CDN path to the first slug. Both use the same functions.

Requirements

  • Any modern browser, Node 20 or newer, workers and edge runtimes. The functions never touch the DOM; the optional HTML layer needs one and does nothing without it.
  • No runtime dependencies.

Install

npm

Terminal
npm install @samline/string

pnpm

Terminal
pnpm add @samline/string

yarn

Terminal
yarn add @samline/string

bun

Terminal
bun add @samline/string

HTML / CDN

Terminal
<script src="https://cdn.jsdelivr.net/npm/@samline/string@2.0.1/dist/browser/string.global.js" defer></script>

Package / ESM

import { camelCase, slugify, truncate } from '@samline/string'

camelCase('user_first_name') // 'userFirstName'
slugify('¡Hola, Ñandú & Café!') // 'hola-nandu-cafe'
truncate('The quick brown fox', 12) // 'The quick…'

Every function is tree-shakeable and available from ESM and CommonJS (require('@samline/string')).

HTML / CDN

For Shopify, WordPress or plain HTML pages without a build step, load the browser bundle with a classic script tag. Pin the version in production.

<script src="https://cdn.jsdelivr.net/npm/@samline/string@2.0.1/dist/browser/string.global.js"></script>
<script>
  const { slugify, titleCase } = window.Strings
</script>

The bundle also starts every data-string-* attribute when the page loads, for example <input data-string="slug" data-string-source="#title" />. See HTML attributes.

Working starter files

A complete page you can save and open: the live preview runs the real package, and its files load the published browser bundle.

Slug fieldLive preview

Attributes only: the slug follows the title until the user edits it, keeps a typed separator while typing, trims on blur and follows again when emptied. The URL preview follows the slug.

example.com/blog/

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Fill a URL slug from the title</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <label for="title">Title</label>
      <input id="title" type="text" value="Café & Té: Guía 2026" />
      <label for="slug">Slug</label>
      <input
        id="slug"
        type="text"
        data-string="slug"
        data-string-source="#title"
        data-string-replacements='{"&": "y"}'
      />
      <p>example.com/blog/<code data-string="slug" data-string-source="#slug"></code></p>
      <p id="status"></p>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/string@2.0.1/dist/browser/string.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
label {
  display: block;
  margin-top: 0.75rem;
  font-weight: 600;
}
input {
  width: 100%;
  max-width: 28rem;
  padding: 0.5rem;
  font: inherit;
}

JavaScript

main.js
// The browser bundle converts every [data-string] element when the page loads.
const slug = document.querySelector('#slug')
const status = document.querySelector('#status')
const show = () =>
  (status.textContent =
    slug.dataset.stringState === 'edited'
      ? 'Edited by hand. Empty the field to follow the title again.'
      : 'Follows the title.')
slug.addEventListener('string:change', show)
slug.addEventListener('input', show)
show()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Fill a URL slug from the title</title>
    <style>
      label {
        display: block;
        margin-top: 0.75rem;
        font-weight: 600;
      }
      input {
        width: 100%;
        max-width: 28rem;
        padding: 0.5rem;
        font: inherit;
      }
    </style>
  </head>
  <body>
    <main>
      <label for="title">Title</label>
      <input id="title" type="text" value="Café & Té: Guía 2026" />
      <label for="slug">Slug</label>
      <input
        id="slug"
        type="text"
        data-string="slug"
        data-string-source="#title"
        data-string-replacements='{"&": "y"}'
      />
      <p>example.com/blog/<code data-string="slug" data-string-source="#slug"></code></p>
      <p id="status"></p>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/string@2.0.1/dist/browser/string.global.js"></script>
    <script>
      // The browser bundle converts every [data-string] element when the page loads.
      const slug = document.querySelector('#slug')
      const status = document.querySelector('#status')
      const show = () =>
        (status.textContent =
          slug.dataset.stringState === 'edited'
            ? 'Edited by hand. Empty the field to follow the title again.'
            : 'Follows the title.')
      slug.addEventListener('string:change', show)
      slug.addEventListener('input', show)
      show()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Fill a URL slug from the title</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <label for="title">Title</label>
      <input id="title" type="text" value="Café & Té: Guía 2026" />
      <label for="slug">Slug</label>
      <input
        id="slug"
        type="text"
        data-string="slug"
        data-string-source="#title"
        data-string-replacements='{"&": "y"}'
      />
      <p>example.com/blog/<code data-string="slug" data-string-source="#slug"></code></p>
      <p id="status"></p>
    </main>
    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
label {
  display: block;
  margin-top: 0.75rem;
  font-weight: 600;
}
input {
  width: 100%;
  max-width: 28rem;
  padding: 0.5rem;
  font: inherit;
}

JavaScript

main.js
import { string } from '@samline/string/vanilla'

string()

const slug = document.querySelector('#slug')
const status = document.querySelector('#status')
const show = () =>
  (status.textContent =
    slug.dataset.stringState === 'edited'
      ? 'Edited by hand. Empty the field to follow the title again.'
      : 'Follows the title.')
slug.addEventListener('string:change', show)
slug.addEventListener('input', show)
show()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Fill a URL slug from the title</title>
    <style>
      label {
        display: block;
        margin-top: 0.75rem;
        font-weight: 600;
      }
      input {
        width: 100%;
        max-width: 28rem;
        padding: 0.5rem;
        font: inherit;
      }
    </style>
  </head>
  <body>
    <main>
      <label for="title">Title</label>
      <input id="title" type="text" value="Café & Té: Guía 2026" />
      <label for="slug">Slug</label>
      <input
        id="slug"
        type="text"
        data-string="slug"
        data-string-source="#title"
        data-string-replacements='{"&": "y"}'
      />
      <p>example.com/blog/<code data-string="slug" data-string-source="#slug"></code></p>
      <p id="status"></p>
    </main>
    <script type="module">
      import { string } from '@samline/string/vanilla'

      string()

      const slug = document.querySelector('#slug')
      const status = document.querySelector('#status')
      const show = () =>
        (status.textContent =
          slug.dataset.stringState === 'edited'
            ? 'Edited by hand. Empty the field to follow the title again.'
            : 'Follows the title.')
      slug.addEventListener('string:change', show)
      slug.addEventListener('input', show)
      show()
    </script>
  </body>
</html>

Next steps