1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:14:58 +00:00

AK: Implement slugify function for URL slug generation

The slugify function is used to convert input into URL-friendly slugs.
It processes each character in the input, keeping ascii alpha characters
after lowercase and replacing non-alphanum characters with the glue
character or a space if multiple spaces are encountered consecutively.
The resulting string is trimmed of leading and trailing whitespace, and
any internal whitespace is replaced with the glue character.

It is currently used in LibMarkdown headings generation code.
This commit is contained in:
Gurkirat Singh 2023-09-27 22:41:57 +05:30 committed by Sam Atkins
parent 670925a84f
commit f1b79e0cd3
5 changed files with 95 additions and 0 deletions

33
AK/Slugify.cpp Normal file
View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2023, Gurkirat Singh <tbhaxor@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/Slugify.h>
#include <AK/StringView.h>
namespace AK {
ErrorOr<String> slugify(String const& input, char const glue)
{
StringBuilder sb;
bool just_processed_space = false;
for (auto const& code_point : input.code_points()) {
if (is_ascii_alphanumeric(code_point)) {
sb.append_code_point(to_ascii_lowercase(code_point));
just_processed_space = false;
} else if ((code_point == static_cast<u32>(glue) || is_ascii_space(code_point)) && !just_processed_space) {
sb.append_code_point(glue);
just_processed_space = true;
}
}
auto output = TRY(sb.to_string());
if (output.ends_with(static_cast<u32>(glue))) {
return output.trim(StringView { &glue, 1 }, TrimMode::Right);
}
return output;
}
}