@danielhaim/slugify

中文文档
1.1.1发布于 2025/09/18MITCommonJS零依赖近 30 天下载 1.9万

A multilingual tool for generating SEO-friendly slugs from strings.

原始使用文档
文档目录11 节
  1. Demo
  2. Supported Languages
  3. Installation
  4. NPM
  5. Examples
  6. Headings with IDs
  7. German special characters
  8. Additional examples with special characters and delimiters
  9. Build Process
  10. Test
  11. Report Bugs

Multilang Slugify

npm version Downloads GitHub

Slugify is an all-purpose slug generator that converts text into clean, SEO-friendly slugs. It's ideal for automating anchor IDs in headings and is designed to handle a wide range of languages and special characters.

Demo

Multilang Slugify Demo

Supported Languages

Slugify supports a diverse set of languages:

German, Danish, Dutch, Finnish, French, Hungarian, Italian, Norwegian, Portuguese, Romanian, Russian, Spanish, Swedish, Turkish, Greek, Bulgarian, Serbian, Croatian, Czech, Polish, Slovak, Slovenian, Latvian, Lithuanian, Estonian, Persian, Arabic, Hebrew, Hindi, Thai, Chinese, Japanese, Korean, Vietnamese, and Ukrainian, as well as Emoji

Installation

You can install this module via npm:

$ npm i @danielhaim/slugify

NPM

import { slugify } from './path/to/slugify/index.js';

const slugifier = new slugify();
console.log(slugifier.generate("Hello World!")); // returns "hello-world"

Examples

Headings with IDs

Input:

<h1><span>How will mobile commerce impact 2022?</span></h1>
<h2><span>Sophia, Ärztin aus Hamburg</span></h2>
<h2><span>Süße Sophia, schön und klug</span></h2>
// Function to generate slugs
function generateSlug(titleElement) {
  const titleContent = titleElement.textContent || '';
  const slugifier = new slugify();
  const slugged = slugifier.generate(titleContent);
  return slugged;
}

// Select all heading elements (h1, h2, h3, etc.)
const headingElements = document.querySelectorAll('h1, h2, h3, h4, h5, h6');

headingElements.forEach((headingElement) => {
  const slugged = generateSlug(headingElement);

  // Only set the ID and create <a> if a valid slug is generated
  if (slugged) {
    const anchorElement = document.createElement('a');
    anchorElement.href = `#${slugged}`;
    anchorElement.textContent = headingElement.textContent;
    headingElement.innerHTML = ''; // Clear existing content
    headingElement.appendChild(anchorElement);
  }
});

Output:

<h1><a href="#how-will-mobile-commerce-impact-2022"><span>How will mobile commerce impact 2022?</span></a></h1>
<h2><a href="#sophia-aerztin-aus-hamburg"><span>Sophia, Ärztin aus Hamburg</span></a></h2>
<h2><a href="#suesse-sophia-schoen-und-klug"><span>Süße Sophia, schön und klug</span></a></h2>

German special characters

const slugifier = new slugify();

slugifier.generate('Ist dein Name Sophia?'); // Output: "ist-dein-name-sophia"
slugifier.generate('Sophia, Ärztin aus Hamburg'); // Output: "sophia-aerztin-aus-hamburg"
slugifier.generate('Wie geht es dir, Sophia?'); // Output: "wie-geht-es-dir-sophia"
slugifier.generate('Süße Sophia, schön und klug'); // Output: "suesse-sophia-schoen-und-klug"
slugifier.generate("Sophia's Geburtstag"); // Output: "sophias-geburtstag"

Additional examples with special characters and delimiters

const slugifier = new slugify();

slugifier.generate('Hello, world!'); // Output: "hello-world"
slugifier.generate('Hello, world!'); // Output: "hello_world"
slugifier.generate('Hello, world!'); // Output: "hello-world"

slugifier.generate('#1 Best in Class'); // Output: "number-1-best-in-class"
slugifier.generate('[email protected]'); // Output: "hello-at-example-com"

Build Process

$ npm run build

Test

$ npm run test

Report Bugs

If you encounter any bugs or issues while using the library, please report them by opening a new issue in the repository's issue tracker.

When reporting a bug, please provide as much detail as possible, including the steps to reproduce the issue and any error messages that you see. I appreciate any contribution to improving this library.

版本与发布记录

@danielhaim/slugify 共发布 9 个版本,首次发布于 2024/01/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.1latest2025/09/181.1 年前
1.1.02025/09/181.1 年前
1.0.112024/01/232 年前
1.0.102024/01/232 年前
1.0.92024/01/232 年前
1.0.82024/01/232 年前

关于 @danielhaim/slugify 的常见问题

@danielhaim/slugify 是什么?

A multilingual tool for generating SEO-friendly slugs from strings.

如何安装 @danielhaim/slugify?

在项目目录运行 npm install @danielhaim/slugify,或使用 pnpm add / yarn add / bun add。

@danielhaim/slugify 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/danielhaim__slugify 是否存在。

@danielhaim/slugify 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@danielhaim/slugify 可以商用吗?

@danielhaim/slugify 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@danielhaim/slugify 的下载量有多少?

根据 npm 官方下载统计,@danielhaim/slugify 在 2026/10/11 – 2026/11/09 这 30 天内被下载 18,980 次,日均约 633 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@danielhaim/slugify 还在维护吗?

最新版本 1.1.1 发布于 2025/09/18(1.1 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/01/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。