Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import XRegExp from 'xregexp'
- import chalk from 'chalk'
- const generateComponentList = (data) => {
- // Loop through a file and locate all component names
- let componentList = []
- XRegExp.forEach(data, XRegExp('<!-- @\\[(\\w+)\\]([.\\w-_+]+)* -->', 'g'), (match) => componentList.push(match))
- console.log(chalk.blue('✅ Found', chalk.bold(componentList.length), 'components'))
- return componentList
- }
- const filterContent = (content) =>
- XRegExp.matchRecursive(content, '<!-- @\\[\\w+\\]', '<!-- @\\[/\\w+\\] -->', 'g', {
- valueNames: ['between', null, null, null]
- })
- const cleanClasses = (classes) => {
- let filteredClasses = (classes) ? classes.split('.') : []
- filteredClasses.shift()
- return filteredClasses
- }
- // Parses text to find components and data
- const componentParser = (data) => {
- // Map the list of component tags and extract data from them
- return generateComponentList(data).map((component) => {
- console.log(chalk.blue('⌛ Processing', component[1], 'component.'))
- const contents = XRegExp.matchRecursive(data, '<!-- @\\[' + component[1] + '\\][.\\w-_+]* -->', '<!-- @\\[/' + component[1] + '\\] -->', 'g')
- let body = ''
- let classes = ''
- contents.map((content) => {
- const filteredContent = filterContent(content)
- body = filteredContent.value
- classes = cleanClasses(component[2])
- console.log(chalk.green('⭐', component[1], 'processing complete.'))
- })
- // Output the content as a JSON object
- return {
- componentName: component[1],
- classes,
- body
- }
- })
- }
- export default componentParser
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement