Skip to content

Commit

Permalink
Change exports, expose svgUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliorizzo committed Mar 21, 2024
1 parent d284872 commit 7253304
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
38 changes: 38 additions & 0 deletions src/svgUtils.ts
Original file line number Diff line number Diff line change
@@ -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(/<!--[\s\S]*?-->/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')
}
37 changes: 2 additions & 35 deletions src/templates/svg/compileSvg.ts
Original file line number Diff line number Diff line change
@@ -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(/<!--[\s\S]*?-->/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)
Expand Down
2 changes: 1 addition & 1 deletion src/templates/svg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,4 @@ export const statusWarning = `<svg version="1.1" baseProfile="tiny" id="Layer_1"
c16.98,0,30.74,13.76,30.74,30.74V1571.3z M1125.28,1165.47c0,16.98-13.76,30.74-30.74,30.74H905.46
c-16.97,0-30.74-13.76-30.74-30.74V658.42c0-16.98,13.76-30.74,30.74-30.74h189.08c16.98,0,30.74,13.76,30.74,30.74V1165.47z"/>
</svg>
`
`

0 comments on commit 7253304

Please sign in to comment.