i have a gen-toc.mjs script that contains my async loop:
/**
 * node imports
 */
import fs from 'fs'
import path from 'path'
/**
 * unified imports
 */
import { unified } from 'unified'
import { visit } from 'unist-util-visit'
/**
 * remark imports
 */
import { remark } from 'remark'
import remarkRehype from 'remark-rehype'
import remarkParse from 'remark-parse'
import remarkToc from 'remark-toc'
/**
 * rehype imports
 */
import rehypeDocument from 'rehype-document'
import rehypeStringify from 'rehype-stringify'
import rehypeRewrite from 'rehype-rewrite'
import rehypeFormat from 'rehype-format'
/**
 * npm imports
 */
import slugify from '@sindresorhus/slugify'
/**
 * local imports
 */
import vivliostyleConfig from '../vivliostyle.config.js'
const readMarkdownFiles = async (mdFilePaths, entryContext) => {
  return await mdFilePaths
    .map(async (md) => {
      const content = await fs.promises.readFile(
        path.join(entryContext, md),
        'utf8'
      )
      console.log({ content })
      return content
    })
    .join('')
}
const main = async () => {
  // read entry from vivliostyle.config.js in order & then change toc.html
  const { title, entry, entryContext, toc, tocTitle } = vivliostyleConfig
  let tocCss = ''
  const mdFilePaths = entry.filter((e) => {
    if (typeof e === 'string') {
      return e
    } else {
      if (e.rel === 'contents') {
        tocCss = e.theme
      }
    }
  })
  const markup = await readMarkdownFiles(mdFilePaths, entryContext)
  console.log({ markup })
  const tree = unified().use(remarkParse).parse(markup)
  visit(tree, (node) => {
    if (node.type === 'heading') {
      console.log(node.children)
    }
  })
}
main()
but it somehow throws an error.
the relevant code looks like:
const readMarkdownFiles = async (mdFilePaths, entryContext) => {
  return await mdFilePaths
    .map(async (md) => {
      const content = await fs.promises.readFile(
        path.join(entryContext, md),
        'utf8'
      )
      console.log({ content })
      return content
    })
    .join('')
}
const main = async () => {
  // read entry from vivliostyle.config.js in order & then change toc.html
  const { title, entry, entryContext, toc, tocTitle } = vivliostyleConfig
  let tocCss = ''
  const mdFilePaths = entry.filter((e) => {
    if (typeof e === 'string') {
      return e
    } else {
      if (e.rel === 'contents') {
        tocCss = e.theme
      }
    }
  })
  // mdFilePaths returns ['chapter1/index.md', 'chapter2/index.md']
  const markup = await readMarkdownFiles(mdFilePaths, entryContext)
  console.log({ markup })
}
but when i run this, i get the following:
{ markup: '[object Promise][object Promise]' }
{
  content: '# chatper2\n' +
    '\n' +
    '## generate pdf, epub, and mobi\n' +
    '\n' +
    "first let's generate pdf.\n" +
    '\n' +
    "then we'll generate an epub.\n" +
    '\n' +
    "finally, we'll generate a mobi.\n" +
    '\n' +
    '{width=100%}\n'
}
{
  content: '# chapter1\n' +
    '\n' +
    '## write a book in markdown\n' +
    '\n' +
    'have a frontmatter.\n' +
    '\n' +
    'frontmatter must contain `title`, `description`, `date`, and `author`.\n' +
    '\n' +
    '{width=100%}'
}
it generates the content loop afterwards of markup as the markup is logged first.
i have tried every single answer & tried many different ways like appending to a string but none of them worked.
i know node.js works like this but unable to solve this even though it's basics.
all i want is to be able to read files contents in order & then append them in one variable markup.
full repro → https://github.com/deadcoder0904/vivliostyle/ (just do npm i and npm run prebuild to run the gen-toc.mjs script)
how can i solve it?
