UNPKG

1.98 kBSource Map (JSON)View Raw
1{"version":3,"file":"markdown-service.modern.mjs","sources":["../src/markdown-service.js"],"sourcesContent":["import fetch from 'node-fetch'\nimport { marked } from 'marked'\n\nconst regExp = /^(-{3}(?:\\n|\\r)([\\w\\W]+?)(?:\\n|\\r)-{3})?([\\w\\W]*)*/\n\nfunction convertMarkdownResponse (response) {\n const contentType = response.headers.get('Content-Type')\n if (contentType.includes('text')) return response.text()\n}\n\nfunction getMarkdownFrontMatter (metas) {\n return metas.split('\\n').reduce((acc, meta) => {\n const [key, value] = meta.trim().split(': ', 2)\n const schema = { [key]: value.slice(1, -1) }\n return { ...acc, ...schema }\n }, {})\n}\n\nfunction formatMarkdownResponseToHtml (data) {\n if (!data) return null\n\n const { 2: metas, 3: body } = regExp.exec(data)\n const content = body ? marked(body) : null\n const head = metas ? getMarkdownFrontMatter(metas) : null\n\n return { head, content }\n}\n\nexport default function markdownService (url) {\n if (!url) return null\n\n return fetch(url)\n .then(convertMarkdownResponse)\n .then(formatMarkdownResponseToHtml)\n .catch(() => null)\n}\n"],"names":["convertMarkdownResponse","response","headers","get","includes","text","formatMarkdownResponseToHtml","data","metas","body","regExp","exec","content","marked","split","reduce","acc","meta","key","value","trim","slice","getMarkdownFrontMatter","head","markdownService","url","fetch","then","catch"],"mappings":"8RAGA,QAAe,qDAEf,SAASA,EAAyBC,GAEhC,GADoBA,EAASC,QAAQC,IAAI,gBACzBC,SAAS,QAAS,OAAOH,EAASI,OAWpD,SAASC,EAA8BC,GACrC,IAAKA,EAAM,OAAA,KAEX,MAAQ,EAAGC,EAAO,EAAGC,GAASC,EAAOC,KAAKJ,GAC7BK,EAAGH,EAAOI,EAAOJ,GAAQ,OACzBD,EAbf,SAAiCA,GAC/B,OAAYA,EAACM,MAAM,MAAMC,OAAO,CAACC,EAAKC,KACpC,MAAOC,EAAKC,GAASF,EAAKG,OAAON,MAAM,KAAM,GAE7C,OAAYE,EAAAA,GAAAA,EADG,CAAEE,CAACA,GAAMC,EAAME,MAAM,GAAI,MAEvC,IAQkBC,CAAuBd,GAAS,KAErD,MAAO,CAAEe,OAAMX,oBAGFY,EAA0BC,GACvC,OAAKA,EAEOC,EAACD,GACVE,KAAK3B,GACL2B,KAAKrB,GACLsB,MAAM,IAAM,MALL"}
\No newline at end of file