Advertisement
Guest User

Untitled

a guest
Nov 10th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import XRegExp from 'xregexp'
  2. import chalk from 'chalk'
  3.  
  4. const generateComponentList = (data) => {
  5.   // Loop through a file and locate all component names
  6.   let componentList = []
  7.   XRegExp.forEach(data, XRegExp('<!-- @\\[(\\w+)\\]([.\\w-_+]+)* -->', 'g'), (match) => componentList.push(match))
  8.   console.log(chalk.blue('✅ Found', chalk.bold(componentList.length), 'components'))
  9.   return componentList
  10. }
  11.  
  12. const filterContent = (content) =>
  13.   XRegExp.matchRecursive(content, '<!-- @\\[\\w+\\]', '<!-- @\\[/\\w+\\] -->', 'g', {
  14.     valueNames: ['between', null, null, null]
  15.   })
  16.  
  17. const cleanClasses = (classes) => {
  18.   let filteredClasses = (classes) ? classes.split('.') : []
  19.   filteredClasses.shift()
  20.   return filteredClasses
  21. }
  22.  
  23. // Parses text to find components and data
  24. const componentParser = (data) => {
  25.   // Map the list of component tags and extract data from them
  26.   return generateComponentList(data).map((component) => {
  27.     console.log(chalk.blue('⌛ Processing', component[1], 'component.'))
  28.     const contents = XRegExp.matchRecursive(data, '<!-- @\\[' + component[1] + '\\][.\\w-_+]* -->', '<!-- @\\[/' + component[1] + '\\] -->', 'g')
  29.     let body = ''
  30.     let classes = ''
  31.  
  32.     contents.map((content) => {
  33.       const filteredContent = filterContent(content)
  34.       body = filteredContent.value
  35.       classes = cleanClasses(component[2])
  36.       console.log(chalk.green('⭐', component[1], 'processing complete.'))
  37.     })
  38.  
  39.     // Output the content as a JSON object
  40.     return {
  41.       componentName: component[1],
  42.       classes,
  43.       body
  44.     }
  45.   })
  46. }
  47.  
  48. export default componentParser
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement