CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/util/news.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { NewsItem } from "./types/news";
7
8
// Slug URL, based on the title and with "-[id]" at the end.
9
// https://www.semrush.com/blog/what-is-a-url-slug/
10
// The main point here is to have a URL that contains unique information and is human readable.
11
export function slugURL(news?: Pick<NewsItem, "id" | "title">): string {
12
if (!news || !news.title || !news.id) return "/news";
13
const { title, id } = news;
14
const shortTitle = title
15
.toLowerCase()
16
// limit the max length, too long URLs are bad as well
17
.slice(0, 200)
18
// replace all non-alphanumeric characters with a space
19
.replace(/[^a-zA-Z0-9]/g, " ")
20
// replace multiple spaces with a single dash
21
.replace(/\s+/g, "-");
22
return `/news/${shortTitle}-${id}`;
23
}
24
25