How to use the svgson.parse function in svgson

To help you get started, we’ve selected a few svgson examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Justineo / vue-awesome-material-icons / build / svg2json.js View on Github external
fs.readdirSync(SVG_DIR).map(async file => {
    console.log(`Converting ${file}...`)
    if (!ICON_PATTERN.test(file)) {
      return
    }
    let fileData = fs.readFileSync(path.resolve(SVG_DIR, file), 'utf8')
    let { error, data } = await svgo.optimize(fileData)
    if (error) {
      console.error(file, error)
      return
    }

    let { attributes, children } = await parse(data)
    let { width, height, viewBox } = attributes
    if (!(width && height)) {
      if (!viewBox) {
        console.error(file, `doesn't contain a valid size declaration.`)
      }

      [width, height] = (viewBox.match(/0 0 (\d+) (\d+)/) || []).map(size => parseInt(size, 10))
    }

    if (!(width && height)) {
      console.error(file, `doesn't contain a valid size declaration.`)
    }

    let icon
    let paths
    if (children.every(({ name }) => name === 'path')) {
github South-Paw / react-vector-maps / maps / convert-svgs.js View on Github external
await asyncForEach(files, async filename => {
      const path = `${this.inputSvgsPath}\\${filename}`;
      const svg = fs.readFileSync(path, 'utf8');

      svgson.parse(svg).then(json => {
        const name = filename.split('.')[0];

        const layers = json.children
          .filter(({ name }) => name === 'path')
          .map(({ attributes }) => ({
            id: attributes.id.toLowerCase(),
            name: attributes.title || capitalize(attributes.id),
            d: attributes.d,
          }));

        const map = {
          id: name,
          name: capitalize(name.split('-').join(' ')),
          viewBox: `0 0 ${parseFloat(json.attributes.width)} ${parseFloat(json.attributes.height)}`,
          layers,
        };
github VictorCazanave / svg-maps / svgToJs.js View on Github external
fs.readFile(svgFile, 'utf8', (err, data) => {
    if (err) {
      console.error(`Unable to read file ${svgFile}`, err)
      return
    }
    console.log(`Parsing file ${svgFile}`)
    svgson.parse(data)
      .then(json => {
        const obj = {
          label: json.attributes['aria-label'],
          viewBox: json.attributes.viewBox,
          locations: json.children
            .filter(child => {
              if (child.name !== 'path') {
                console.warn(`<${child.name}> tag will be ignored`)
                return false
              }

              return true
            })
            .map(child => ({
              name: child.attributes.name,
              id: child.attributes.id,

svgson

Transform svg files into Objects

MIT
Latest version published 10 months ago

Package Health Score

64 / 100
Full package analysis