generated from polymech/site-template
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { slug } from "github-slugger"
|
|
import { marked } from "marked"
|
|
export const slugify = (content: string) => slug(content)
|
|
|
|
export const markdownify = (content: string, div?: boolean) => {
|
|
return div ? marked.parse(content) : marked.parseInline(content)
|
|
}
|
|
|
|
export const humanize = (content: string) => {
|
|
return content
|
|
.replace(/^[\s_]+|[\s_]+$/g, "")
|
|
.replace(/[_\s]+/g, " ")
|
|
.replace(/[-\s]+/g, " ")
|
|
.replace(/^[a-z]/, function (m) {
|
|
return m.toUpperCase();
|
|
})
|
|
}
|
|
|
|
export const titleify = (content: string) => {
|
|
const humanized = humanize(content);
|
|
return humanized
|
|
.split(" ")
|
|
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
.join(" ");
|
|
}
|
|
|
|
export const plainify = (content: string) => {
|
|
const parseMarkdown: any = marked.parse(content);
|
|
const filterBrackets = parseMarkdown.replace(/<\/?[^>]+(>|$)/gm, "");
|
|
const filterSpaces = filterBrackets.replace(/[\r\n]\s*[\r\n]/gm, "");
|
|
const stripHTML = htmlEntityDecoder(filterSpaces);
|
|
return stripHTML
|
|
}
|
|
|
|
// strip entities for plainify
|
|
const htmlEntityDecoder = (htmlWithEntities: string) => {
|
|
let entityList: { [key: string]: string } = {
|
|
" ": " ",
|
|
"<": "<",
|
|
">": ">",
|
|
"&": "&",
|
|
""": '"',
|
|
"'": "'",
|
|
};
|
|
let htmlWithoutEntities: string = htmlWithEntities.replace(
|
|
/(&|<|>|"|')/g,
|
|
(entity: string): string => {
|
|
return entityList[entity];
|
|
},
|
|
);
|
|
return htmlWithoutEntities;
|
|
}
|