From 7253304858a81f0509a32c52068778913400c557 Mon Sep 17 00:00:00 2001 From: emi Date: Thu, 21 Mar 2024 14:35:56 +0000 Subject: [PATCH] Change exports, expose svgUtils --- src/index.ts | 1 + src/svgUtils.ts | 38 +++++++++++++++++++++++++++++++++ src/templates/svg/compileSvg.ts | 37 ++------------------------------ src/templates/svg/index.ts | 2 +- 4 files changed, 42 insertions(+), 36 deletions(-) create mode 100644 src/svgUtils.ts diff --git a/src/index.ts b/src/index.ts index d274e6d..965cb12 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ export { MdParser, setupMarkdownIt } from './MdParser' export * as findings from './Findings' export * as templates from './templates/mdTemplates' +export * as svgUtils from './svgUtils' diff --git a/src/svgUtils.ts b/src/svgUtils.ts new file mode 100644 index 0000000..26fe3bd --- /dev/null +++ b/src/svgUtils.ts @@ -0,0 +1,38 @@ +import * as fs from 'fs' +import * as path from 'path' + +export const removeXMLdec = (xml: string) => + xml.replace(/^<\?xml\s+version="1\.0"\s+encoding="utf-8"\s*\?>/i, '') + +export const removeXmlComments = (xml: string) => + xml.replace(//g, '') + +export const cleanSVG = (svg: string) => { + svg = removeXMLdec(svg) + svg = removeXmlComments(svg) + svg = svg.replace(/^\s*\n/gm, '') + return svg +} + +export const compileSvgs = (dir: string) => { + const files = fs.readdirSync(dir) + const svgFiles = files.filter((file) => path.extname(file) === '.svg') + const svgVariables: string[] = [] + + svgFiles.forEach((file) => { + const svgContent = cleanSVG(fs.readFileSync(path.join(dir, file), 'utf-8')) + + const variableName = path + .basename(file, '.svg') + .replace(/[^a-zA-Z0-9]/g, '_') + svgVariables.push(`export const ${variableName} = \`${svgContent}\``) + }) + + const tsContent = svgVariables.join('\n\n') + return tsContent +} + +export const writeCompiledSvg = (dir: string, out: string) => { + const tsContent = compileSvgs(dir) + fs.writeFileSync(out, tsContent, 'utf-8') +} diff --git a/src/templates/svg/compileSvg.ts b/src/templates/svg/compileSvg.ts index 3d35de9..803ff95 100644 --- a/src/templates/svg/compileSvg.ts +++ b/src/templates/svg/compileSvg.ts @@ -1,42 +1,9 @@ -import * as fs from 'fs' -import * as path from 'path' - +import { writeCompiledSvg } from '../../svgUtils' const svgPath = __dirname const outputTsFile = `${__dirname}/index.ts` -const removeXMLdec = (xml: string) => - xml.replace(/^<\?xml\s+version="1\.0"\s+encoding="utf-8"\s*\?>/i, '') - -const removeXmlComments = (xml: string) => xml.replace(//g, '') - -const cleanSVG = (svg: string) => { - svg = removeXMLdec(svg) - svg = removeXmlComments(svg) - svg = svg.replace(/^\s*\n/gm, '') - return svg -} - -function compileSvgs(dir: string, out: string) { - const files = fs.readdirSync(dir) - const svgFiles = files.filter((file) => path.extname(file) === '.svg') - const svgVariables: string[] = [] - - svgFiles.forEach((file) => { - const svgContent = cleanSVG(fs.readFileSync(path.join(dir, file), 'utf-8')) - - const variableName = path - .basename(file, '.svg') - .replace(/[^a-zA-Z0-9]/g, '_') - svgVariables.push(`export const ${variableName} = \`${svgContent}\``) - }) - - const tsContent = svgVariables.join('\n\n') - - fs.writeFileSync(out, tsContent, 'utf-8') -} - try { - compileSvgs(svgPath, outputTsFile) + writeCompiledSvg(svgPath, outputTsFile) console.log(`SVGs have been compiled into ${outputTsFile}`) } catch (err) { console.error('Error compiling SVGs:', err) diff --git a/src/templates/svg/index.ts b/src/templates/svg/index.ts index 8c8e7b0..01561f2 100644 --- a/src/templates/svg/index.ts +++ b/src/templates/svg/index.ts @@ -195,4 +195,4 @@ export const statusWarning = ` -` \ No newline at end of file +`