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/next/lib/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 LRU from "lru-cache";
7
8
// no TTL, since a given text never changes its rendered representation
9
const renderCache = new LRU<string, string>({ max: 100 });
10
11
import { sha1 } from "@cocalc/backend/sha1";
12
import { markdown_to_html } from "@cocalc/frontend/markdown";
13
14
export function renderMarkdown(text: string): string {
15
const key = sha1(text);
16
const cached = renderCache.get(key);
17
if (cached) return cached;
18
const html = markdown_to_html(text);
19
renderCache.set(key, html);
20
return html;
21
}
22
23
export function extractID(
24
param: string | string[] | undefined
25
): number | undefined {
26
// if id is null or does not start with an integer, return 404
27
if (param == null || typeof param !== "string") return;
28
// we support URLs with a slug and id at the end, e.g., "my-title-1234"
29
// e.g. https://www.semrush.com/blog/what-is-a-url-slug/
30
const idStr = param.split("-").pop();
31
const id = Number(idStr);
32
if (!Number.isInteger(id) || id < 0) return;
33
return id;
34
}
35
36