svg.mjs 915 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import glob from 'glob'
  2. import matter from 'gray-matter'
  3. const currentDirPath = path.dirname(fileURLToPath(import.meta.url))
  4. const getRawIconData = (content) => {
  5. return content
  6. .replace('<svg>', '')
  7. .replace('</svg>', '')
  8. .replace(/\n\s+/g, '')
  9. .trim()
  10. }
  11. export const getIcons = () => {
  12. return glob.sync(path.join(currentDirPath, '../src/*.svg')).map(icon => {
  13. const svg = fs.readFileSync(icon, 'utf-8'),
  14. name = path.basename(icon, '.svg')
  15. const { content, data } = matter(svg)
  16. return {
  17. name,
  18. content: getRawIconData(content, name),
  19. ...data
  20. }
  21. })
  22. }
  23. export const groupByCategory = (items) => {
  24. return Object.values(items.reduce((group, item) => {
  25. const { category } = item;
  26. group[category] = group[category] ?? {
  27. name: category,
  28. items: []
  29. };
  30. group[category].items.push(item);
  31. return group;
  32. }, {}))
  33. }