|
| 1 | +const _ = require('lodash') |
1 | 2 | const frontMatter = require('front-matter') |
2 | 3 | const slug = require('slug') |
3 | | -const { |
4 | | - templateExtensions, |
5 | | - isTemplateFile, |
6 | | - removeExtension |
7 | | -} = require('../helpers') |
| 4 | +const { templateExtensions, removeExtension } = require('../helpers') |
8 | 5 |
|
9 | 6 | const models = { |
10 | 7 | attachment: require('./attachment') |
11 | 8 | } |
12 | 9 |
|
13 | | -function _baseEntry(node, indexFileNameOptions) { |
14 | | - const dotlessTemplateExtensions = templateExtensions.map(e => e.substring(1)) |
15 | | - const entryFile = node.children ? |
16 | | - node.children.find(child => { |
17 | | - return isTemplateFile(child) && child.name.match( |
18 | | - new RegExp(`^(${indexFileNameOptions.join('|')})\.(${dotlessTemplateExtensions.join('|')})$`, 'i') |
19 | | - ) |
20 | | - }) : |
21 | | - node |
| 10 | +const isIndexFile = (node, nameOptions) => { |
| 11 | + const names = nameOptions.join('|') |
| 12 | + const extensions = templateExtensions.join('|') |
| 13 | + const namePattern = new RegExp(`^(${names})(${extensions})$`, 'i') |
| 14 | + return !node.children && node.name.match(namePattern) |
| 15 | +} |
| 16 | + |
| 17 | +function parseFolderedEntry(node, indexFileNameOptions) { |
| 18 | + const tree = { |
| 19 | + indexFile: null, |
| 20 | + attachments: [] |
| 21 | + } |
| 22 | + node.children.forEach(childNode => { |
| 23 | + if (isIndexFile(childNode, indexFileNameOptions)) { |
| 24 | + tree.indexFile = childNode |
| 25 | + return |
| 26 | + } |
| 27 | + tree.attachments.push(models.attachment.bind(null, childNode)) |
| 28 | + }) |
22 | 29 |
|
23 | | - const attachments = (node.children || []) |
24 | | - .filter(child => child !== entryFile) |
25 | | - .map(child => models.attachment(child)) |
| 30 | + return { |
| 31 | + ...tree, |
| 32 | + name: node.name |
| 33 | + } |
| 34 | +} |
26 | 35 |
|
| 36 | +function _baseEntry(node, indexFileNameOptions) { |
| 37 | + const folderedEntry = node.children ? |
| 38 | + parseFolderedEntry(node, indexFileNameOptions) : |
| 39 | + undefined |
| 40 | + const entryFile = folderedEntry?.indexFile || node |
27 | 41 | const { attributes, body } = frontMatter(entryFile.content) |
| 42 | + const entryName = removeExtension( |
| 43 | + folderedEntry?.name || entryFile.name |
| 44 | + ) |
| 45 | + const attachments = folderedEntry?.attachments || [] |
28 | 46 |
|
29 | 47 | return { |
30 | | - ...node, |
| 48 | + ..._.omit(node, 'children'), |
31 | 49 | ...attributes, |
32 | | - title: attributes.title || removeExtension(node.children ? node.name : entryFile.name), |
33 | | - slug: attributes.slug || slug(removeExtension(node.children ? node.name : entryFile.name), '-'), |
| 50 | + title: attributes.title || entryName, |
| 51 | + slug: attributes.slug || slug(entryName), |
34 | 52 | content: body || '', |
35 | 53 | attachments |
36 | 54 | } |
|
0 commit comments