chore: format code & add format tools.

This commit is contained in:
jaywcjlove
2022-09-02 22:45:28 +08:00
parent 6da12fcb10
commit d007e11f20
12 changed files with 157 additions and 85 deletions

View File

@@ -4,25 +4,25 @@ export const getBlock = (data: any, str: string = '') => {
if (data && data.data && data.data.type === 'Declaration') {
str = `${data.data.property}: ${data.data.value.value}${data.data.important ? ' !important' : ''};`;
if (data.next) {
str += getBlock(data.next)
str += getBlock(data.next);
}
}
return str;
}
};
export const cssdata = (list: any, result: Record<string, string> = {}) => {
if (list.data && list.data.type === 'Rule') {
result[list.data.prelude.value] = getBlock(list.data.block.children.head);
if (list.next) {
result = cssdata(list.next, {...result})
result = cssdata(list.next, { ...result });
}
}
return result;
}
};
export const spaceEscape = (node: RootContent) => {
if (node.type === 'element' && node.children) {
const className = (node.properties?.className as string[]);
const className = node.properties?.className as string[];
if (className) {
if (!node.properties) {
node.properties = {};
@@ -30,17 +30,17 @@ export const spaceEscape = (node: RootContent) => {
node.properties.className = className.filter((str: string) => !/(token|control-flow)/.test(str));
}
node.children.map(elm => {
node.children.map((elm) => {
if (elm.type === 'element' && elm.children) {
spaceEscape(elm)
spaceEscape(elm);
}
if (elm.type === 'text') {
elm.value = elm.value.replace(/\s/g, '\u00A0')
elm.value = elm.value.replace(/\s/g, '\u00A0');
}
return elm
})
return elm;
});
}
}
};
type ChildContent = Element | Text;
const getNodeText = (node: ChildContent[]) => {
@@ -50,53 +50,64 @@ const getNodeText = (node: ChildContent[]) => {
else if (item.type === 'element') {
str += getNodeText(item.children as ChildContent[]);
}
})
});
return str.replace(/↩/, '');
}
};
export const footnotes = (node: Element) => {
node.children.map((item) => {
if (item.type === 'element' && item.tagName === 'h2') {
if (!item.properties) item.properties = {};
item.properties.className = ['footnotes-title'];
item.children = [{
type: 'text',
value: '参考'
}];
item.children = [
{
type: 'text',
value: '参考',
},
];
}
if (item.type === 'element' && item.tagName === 'ol') {
item.children.map((li) => {
if (li.type === 'element' && li.tagName === 'li') {
if (!li.properties) li.properties = {};
li.properties.className = ['footnotes-list'];
li.children = [{
type: 'text',
value: getNodeText(li.children as ChildContent[])
}]
li.children = [
{
type: 'text',
value: getNodeText(li.children as ChildContent[]),
},
];
}
return li;
})
});
}
return item;
})
}
});
};
export const footnotesLabel = (node: Element) => {
const label = getNodeText(node.children as ChildContent[]);
node.children = [{
type: 'text',
value: `[${label}]`
}];
}
node.children = [
{
type: 'text',
value: `[${label}]`,
},
];
};
export const imagesStyle = (node: Element, parent: Root | Element | null) => {
if (parent?.type === 'element' && /(p|a)/.test(parent.tagName) && node?.type === 'element' && node.tagName === 'img') {
if (
parent?.type === 'element' &&
/(p|a)/.test(parent.tagName) &&
node?.type === 'element' &&
node.tagName === 'img'
) {
if (parent.tagName === 'p') {
parent.tagName = 'figure'
parent.tagName = 'figure';
}
if (!parent.properties) parent.properties = {}
parent.properties.className = ['image-warpper']
if (!node.properties) node.properties = {}
node.properties.className = ['image']
if (!parent.properties) parent.properties = {};
parent.properties.className = ['image-warpper'];
if (!node.properties) node.properties = {};
node.properties.className = ['image'];
}
}
};

View File

@@ -13,17 +13,15 @@ import rehypeRewrite from 'rehype-rewrite';
import stringify from 'rehype-stringify';
import { cssdata, spaceEscape, footnotes, footnotesLabel, imagesStyle } from './css';
export type MarkdownToHTMLOptions = {
}
export type MarkdownToHTMLOptions = {};
export function markdownToHTML(md: string, css: string, options: MarkdownToHTMLOptions = {}) {
const ast = csstree.parse(css, {
const ast = csstree.parse(css, {
parseAtrulePrelude: false,
parseRulePrelude: false,
parseValue: false,
parseCustomProperty: false,
positions: false
positions: false,
});
// @ts-ignore
const data = cssdata(ast.children.head);
@@ -33,32 +31,46 @@ export function markdownToHTML(md: string, css: string, options: MarkdownToHTMLO
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypePrism)
.use(rehypeRaw)
.use(rehypeIgnore, { })
.use(rehypeIgnore, {})
.use(rehypeAttrs, { properties: 'attr' })
.use(rehypeRewrite, {
rewrite: (node, index, parent) => {
// @ts-ignore
if (node?.type === 'element' && node?.tagName === 'code' && parent?.type === 'element' && parent?.tagName === 'pre') {
spaceEscape(node)
if (
node?.type === 'element' &&
node?.tagName === 'code' &&
parent?.type === 'element' &&
parent?.tagName === 'pre'
) {
spaceEscape(node);
}
if (node?.type === 'element' && node.tagName === 'section' && (node?.properties?.className as string[]).includes('footnotes')) {
footnotes(node)
if (
node?.type === 'element' &&
node.tagName === 'section' &&
(node?.properties?.className as string[]).includes('footnotes')
) {
footnotes(node);
}
if (node?.type === 'element' && node.tagName === 'sup') {
footnotesLabel(node)
footnotesLabel(node);
}
if (node?.type === 'element' && node.tagName === 'img') {
imagesStyle(node, parent)
imagesStyle(node, parent);
}
// Code Spans style
if (node?.type === 'element' && node?.tagName === 'code' && parent?.type === 'element' && parent?.tagName !== 'pre') {
if (!node.properties) node.properties = {}
if (
node?.type === 'element' &&
node?.tagName === 'code' &&
parent?.type === 'element' &&
parent?.tagName !== 'pre'
) {
if (!node.properties) node.properties = {};
node.properties!.className = ['code-spans'];
}
// List TODO style
if (parent?.type === 'element' && node?.type === 'element' && node?.tagName === 'input') {
if (parent?.type === 'element' && node?.type === 'element' && node?.tagName === 'input') {
if (parent && parent.type === 'element') {
parent.children = parent?.children.filter(elm => (elm as Element).tagName !== 'input')
parent.children = parent?.children.filter((elm) => (elm as Element).tagName !== 'input');
}
return;
}
@@ -67,25 +79,25 @@ export function markdownToHTML(md: string, css: string, options: MarkdownToHTMLO
if (!node.properties) {
node.properties = {};
}
const className = (node.properties?.className as string[]);
const className = node.properties?.className as string[];
let style = '';
if (className) {
className.forEach((name) => {
if (data[`.${name}`]) {
style = data[`.${name}`];
}
})
});
}
if (!style) style = data[node.tagName];
if (style) {
node.properties.style = style + (node.properties.style || '');
}
}
}
},
})
.use(stringify);
const file = new VFile();
file.value = md;
const hastNode = processor.runSync(processor.parse(file), file);
return String(processor.stringify(hastNode, file));
const file = new VFile();
file.value = md;
const hastNode = processor.runSync(processor.parse(file), file);
return String(processor.stringify(hastNode, file));
}