- Docs
- Getting started
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
npm install @samline/stringpnpm
pnpm add @samline/stringyarn
yarn add @samline/stringbun
bun add @samline/stringHTML / CDN
<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.
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
<!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
label {
display: block;
margin-top: 0.75rem;
font-weight: 600;
}
input {
width: 100%;
max-width: 28rem;
padding: 0.5rem;
font: inherit;
}
JavaScript
// 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
<!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
<!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
label {
display: block;
margin-top: 0.75rem;
font-weight: 600;
}
input {
width: 100%;
max-width: 28rem;
padding: 0.5rem;
font: inherit;
}
JavaScript
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
<!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
Live previews with the complete, copyable source of each one.
API referenceEvery function with examples.
ConfigurationDefaults and accepted values of every function, the HTML layer and locales.
Word splittingExactly how words are detected.
HTML attributesConvert text and form fields with data-string.
Accent-insensitive matching, highlighting, natural sorting and list filtering.