UNPKG

@subsocial/utils

Version:
35 lines (34 loc) 1.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPostIdFromSlug = exports.createPostSlug = void 0; const string_1 = require("./string"); const slugify_1 = __importDefault(require("@sindresorhus/slugify")); const summarize_1 = require("./summarize"); const MAX_SLUG_LENGTH = 60; const SLUG_SEPARATOR = '-'; /** Create slug from the content title or body, appended with the id at the end */ const createPostSlug = (postId, content) => { let slug = '' + postId; if (content) { const { title, body } = content; const titleOrBody = (0, string_1.nonEmptyStr)(title) ? title : body; const summary = (0, summarize_1.summarize)(titleOrBody, { limit: MAX_SLUG_LENGTH, omission: '' }); const slugifiedSummary = (0, slugify_1.default)(summary, { separator: SLUG_SEPARATOR }); if ((0, string_1.nonEmptyStr)(slugifiedSummary)) { slug = slugifiedSummary + '-' + slug; } } return slug; }; exports.createPostSlug = createPostSlug; /** Extract post id from the slug generated from `createPostSlug` */ const getPostIdFromSlug = (slug) => { return slug.split(SLUG_SEPARATOR).pop(); }; exports.getPostIdFromSlug = getPostIdFromSlug;