Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/lowmess/lameass.recipes. A recipe site, but without the ads and 3,000 word essays lameass.recipes
nextjs sanity-io theme-ui typescript
Something went wrong. Try again.
902 B · 35 lines
TypeScript
at develop
123456789101112131415161718192021222324252627282930313233343536import { remark } from 'remark'import html from 'remark-html'import smartypants from './smartypants'
// Should probably do this in a remark plugin, but I don't wannaconst noteToSup = (str: string): string => { const note = /[[](\d)+[\]]/g
return str.replace(note, '<sup>$1</sup>')}
const toInline = (str: string, allowLineBreaks = false): string => { const wrappers = /^(<p>)|(<\/p>)$/g const breaks = /(<\/p>){1}[\s]*(<p>){1}/g
return str .trim() .replace(wrappers, '') .replace(breaks, allowLineBreaks ? '<br /><br />' : ' ')}
type Options = { inline?: boolean allowLineBreaks?: boolean}
const mdToHTML = (string: string, options: Options = {}): string => { const md = remark().use(html).processSync(string).toString()
if (!options.inline) return smartypants(noteToSup(md))
return smartypants(toInline(noteToSup(md), options.allowLineBreaks))}
export default mdToHTML