Skip to content

Commit

Permalink
feat: 优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
nmsn committed Jun 27, 2023
1 parent 0d74ec3 commit 8559b01
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 108 deletions.
57 changes: 0 additions & 57 deletions src/helper.ts

This file was deleted.

59 changes: 59 additions & 0 deletions src/md.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os from 'os';

export const LF = os.EOL;
const ParagraphDividingLine = `${LF}${LF}`;

const paragraph = (content: string[]) => {
return content.join(ParagraphDividingLine);
};

const heading = (title: string, level: number) => {
return `${'#'.repeat(level)} ${title}`;
};

const blockquote = (text: string) => {
return `> ${text}`;
};

const link = (title: string, url: string) => {
return `[${title}](${url})`;
};

const table = (header: string[], content: string[][]) => {
const content2TableRow = (row: string[]) => {
return `|${row.join('|')}|`;
};

const headerRow = content2TableRow(header);

const divider = content2TableRow(Array(header?.length).fill('---'));

const contentRow = content.map((item) => content2TableRow(item));

return [headerRow, divider, ...contentRow].join(LF);
};

type HeadingParamsType = { type: 'h1' | 'h2' | 'h3' | 'h4' | 'h5'; params: string };
type BlockquoteParamsType = { type: 'blockquote'; params: string };
type LinkParamsType = { type: 'link'; params: [title: string, url: string] };
type TableParamsType = { type: 'table'; params: [header: string[], content: string[][]] };

type DataType = (HeadingParamsType | BlockquoteParamsType | LinkParamsType | TableParamsType)[];

export const write = (data: DataType) => {
const headingMap = Array(5)
.fill(undefined)
.map((_, index) => index + 1)
.map((item) => [`h${item}`, (title: string) => heading(title, item)]);

const headingObj = Object.fromEntries(headingMap);

const funcMap = {
...headingObj,
blockquote,
link,
table,
};

return paragraph(data.map(({ type, params }) => funcMap[type](...params)));
};
24 changes: 24 additions & 0 deletions src/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { writeFileToLine } from './utils.js';
import { write } from './md.js';

writeFileToLine(
'TEST.md',
write([
{ type: 'h1', params: '123' },
{ type: 'h2', params: '123' },
{ type: 'h3', params: '123' },
{ type: 'h3', params: '123' },
{ type: 'blockquote', params: '123' },
{ type: 'link', params: ['123', '123'] },
{
type: 'table',
params: [
['测试', '测试2'],
[
['123', '124'],
['dasd', 'asd'],
],
],
},
])
);
67 changes: 16 additions & 51 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,24 @@
import os from 'os';
import fs from 'fs';
import path from 'path';

export const LF = os.EOL;
export const ParagraphDividingLine = `${LF}${LF}`;
import { LF } from './md.js';

const paragraph = (content: string[]) => {
return content.join(ParagraphDividingLine);
};

const heading = (title: string, level: number) => {
return `${'#'.repeat(level)} ${title}`;
};

const blockquote = (text: string) => {
return `> ${text}`;
};

const link = (title: string, url: string) => {
return `[${title}](${url})`;
};
//往固定的行写入数据
export const writeFileToLine = (fileName: string, content: string, line = 0) => {
const _path = path.resolve('./', fileName);

const table = (header: string[], content: string[][]) => {
const content2TableRow = (row: string[]) => {
return `|${row.join('|')}|`;
};
// 计算行
const rows = fs.readFileSync(_path, 'utf8').split(LF);
const _content = (!content.endsWith(LF) ? `${content}${LF}` : content).split(LF);

const headerRow = content2TableRow(header);
// 在相应的行插入内容
rows.splice(line, 0, ..._content);
const outputMd = rows.map((item) => (item === '' ? LF : `${item}${LF}`)).join('');

const divider = content2TableRow(Array(header?.length).fill('---'));

const contentRow = content.map((item) => content2TableRow(item));

return [headerRow, divider, ...contentRow].join(LF);
fs.writeFileSync(_path, outputMd);
};

type HeadingParamsType = { type: 'h1' | 'h2' | 'h3' | 'h4' | 'h5'; params: string };
type BlockquoteParamsType = { type: 'blockquote'; params: string };
type LinkParamsType = { type: 'link'; params: [title: string, url: string] };
type TableParamsType = { type: 'table'; params: [header: string[], content: string[][]] };

type DataType = (HeadingParamsType | BlockquoteParamsType | LinkParamsType | TableParamsType)[];

export const write = (data: DataType) => {
const headingMap = Array(5)
.fill(undefined)
.map((_, index) => index + 1)
.map((item) => [`h${item}`, (title: string) => heading(title, item)]);

const headingObj = Object.fromEntries(headingMap);

const funcMap = {
...headingObj,
blockquote,
link,
table,
};

return paragraph(data.map(({ type, params }) => funcMap[type](...params)));
export const createNewFile = (fileName: string, content: string) => {
const _path = path.resolve('./', fileName);
fs.writeFileSync(_path, content);
};

0 comments on commit 8559b01

Please sign in to comment.