2022-09-26 09:52:59 +08:00
|
|
|
import markdown from '@wcj/markdown-to-html';
|
|
|
|
import rehypeDocument from 'rehype-document';
|
2022-09-30 23:18:13 +08:00
|
|
|
import remarkGemoji from 'remark-gemoji';
|
|
|
|
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
|
|
|
|
import rehypeSlug from 'rehype-slug';
|
2022-09-26 17:13:24 +08:00
|
|
|
import { htmlTagAddAttri } from './nodes/htmlTagAddAttri.mjs';
|
2022-09-26 22:14:52 +08:00
|
|
|
import { footer } from './nodes/footer.mjs';
|
2022-09-27 23:09:51 +08:00
|
|
|
import { header } from './nodes/header.mjs';
|
2022-09-29 22:46:01 +08:00
|
|
|
import { rehypeUrls } from './utils/rehypeUrls.mjs';
|
|
|
|
import { tooltips } from './utils/tooltips.mjs';
|
2022-09-30 14:15:26 +08:00
|
|
|
import { homeCardIcons } from './utils/homeCardIcons.mjs';
|
2022-10-01 02:29:00 +08:00
|
|
|
import { getTocsTree } from './utils/getTocsTree.mjs';
|
2022-10-15 13:32:12 +08:00
|
|
|
import { rehypeTitle } from './utils/rehypeTitle.mjs';
|
2022-10-16 02:22:53 +08:00
|
|
|
import { anchorPoint } from './utils/anchorPoint.mjs';
|
2022-09-26 09:52:59 +08:00
|
|
|
|
2022-10-16 01:21:33 +08:00
|
|
|
const favicon = `data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E`;
|
2022-09-30 22:45:49 +08:00
|
|
|
|
2022-09-26 09:52:59 +08:00
|
|
|
export function create(str = '', options = {}) {
|
2022-09-30 00:26:09 +08:00
|
|
|
let title = str.match(/[^===]+(?=[===])/g) || [];
|
|
|
|
let description = str.match(/\n==={1,}\n+([\s\S]*?)\n/g) || [];
|
2022-09-27 23:09:51 +08:00
|
|
|
title = title[0] || '';
|
|
|
|
description = (description[0] || '').replace(/^\n[=\n]+/, '').replace(/\[([\s\S]*?)?\]\(([\s\S]*?)?\)/g, '$1').replace(/\n/, '');
|
2022-09-28 02:12:32 +08:00
|
|
|
const subTitle = options.filename && !options.isHome ? `${options.filename} cheatsheet & `: ''
|
2022-09-26 09:52:59 +08:00
|
|
|
const mdOptions = {
|
2022-10-05 14:46:37 +08:00
|
|
|
showLineNumbers: false,
|
2022-09-26 09:52:59 +08:00
|
|
|
hastNode: false,
|
2022-09-29 22:46:01 +08:00
|
|
|
remarkPlugins: [remarkGemoji],
|
2022-09-26 09:52:59 +08:00
|
|
|
rehypePlugins: [
|
2022-09-30 23:18:13 +08:00
|
|
|
rehypeSlug,
|
|
|
|
rehypeAutolinkHeadings,
|
2022-09-26 09:52:59 +08:00
|
|
|
[rehypeDocument, {
|
2022-09-28 02:12:32 +08:00
|
|
|
title: `${title ? `${title} & ` : ''} ${subTitle} Quick Reference`,
|
2022-09-26 09:52:59 +08:00
|
|
|
css: [ ...options.css ],
|
2022-09-30 22:45:49 +08:00
|
|
|
link: [
|
|
|
|
{rel: 'icon', href: favicon, type: 'image/svg+xml'}
|
|
|
|
],
|
2022-09-26 09:52:59 +08:00
|
|
|
meta: [
|
2022-09-27 23:09:51 +08:00
|
|
|
{ description: `${description}为开发人员分享快速参考备忘单。` },
|
2022-09-28 02:12:32 +08:00
|
|
|
{ keywords: `Quick,Reference,cheatsheet,${!options.isHome && options.filename || ''}` }
|
2022-09-26 09:52:59 +08:00
|
|
|
]
|
|
|
|
}],
|
|
|
|
],
|
|
|
|
rewrite: (node, index, parent) => {
|
2022-10-15 13:32:12 +08:00
|
|
|
rehypeTitle(node, options.filename);
|
2022-09-30 14:15:26 +08:00
|
|
|
homeCardIcons(node, parent, options.isHome);
|
2022-09-29 22:46:01 +08:00
|
|
|
tooltips(node, index, parent);
|
2022-09-27 23:09:51 +08:00
|
|
|
htmlTagAddAttri(node, options);
|
2022-09-26 17:13:24 +08:00
|
|
|
rehypeUrls(node);
|
2022-09-26 09:52:59 +08:00
|
|
|
if (node.type === 'element' && node.tagName === 'body') {
|
|
|
|
node.children = getTocsTree([ ...node.children ]);
|
2022-09-27 23:09:51 +08:00
|
|
|
node.children.unshift(header(options));
|
2022-09-26 22:14:52 +08:00
|
|
|
node.children.push(footer());
|
2022-10-16 02:22:53 +08:00
|
|
|
node.children.push(anchorPoint());
|
2022-09-26 09:52:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return markdown(str, mdOptions);
|
|
|
|
}
|