From 8559b01d2449dec638a6ca130c4806404e976394 Mon Sep 17 00:00:00 2001 From: nmsn <136696700@qq.com> Date: Tue, 27 Jun 2023 18:01:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/helper.ts | 57 ------------------------------------------- src/md.ts | 59 +++++++++++++++++++++++++++++++++++++++++++++ src/test.js | 24 ++++++++++++++++++ src/utils.ts | 67 ++++++++++++--------------------------------------- 4 files changed, 99 insertions(+), 108 deletions(-) delete mode 100644 src/helper.ts create mode 100644 src/md.ts create mode 100644 src/test.js diff --git a/src/helper.ts b/src/helper.ts deleted file mode 100644 index 0ec7773..0000000 --- a/src/helper.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { write, LF } from './utils.js'; - -import fs from 'fs'; -import path from 'path'; - -//往固定的行写入数据 -export const writeFileToLine = (fileName: string, content: string, line = 0) => { - const _path = path.resolve('./', fileName); - - const rowWithBlankLine = fs.readFileSync(_path, 'utf8').split(/\r\n|\n|\r/gm); //readFileSync的第一个参数是文件名 - - const newContent = (() => { - if (!content.endsWith(LF)) { - return `${content}${LF}`; - } - - return content; - })(); - const _content = newContent.split(/\r\n|\n|\r/gm); - rowWithBlankLine.splice(line, 0, ..._content); - - const result = rowWithBlankLine.map((item) => { - if (item === '') { - return LF; - } - - return `${item}${LF}`; - }); - fs.writeFileSync(_path, result.join('')); -}; - -export const createNewFile = (fileName: string, content: string) => { - const _path = path.resolve('./', fileName); - fs.writeFileSync(_path, content); -}; - -writeFileToLine( - 'README3.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'], - ], - ], - }, - ]) -); diff --git a/src/md.ts b/src/md.ts new file mode 100644 index 0000000..c92cf78 --- /dev/null +++ b/src/md.ts @@ -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))); +}; diff --git a/src/test.js b/src/test.js new file mode 100644 index 0000000..30b17e4 --- /dev/null +++ b/src/test.js @@ -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'], + ], + ], + }, + ]) +); diff --git a/src/utils.ts b/src/utils.ts index e908ff0..ea31b79 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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); };