求救!如何用gulp生成文件路径并渲染到a标签里面
发布于 7 年前 作者 lijianhu1 3466 次浏览 来自 问答

各位大神,就是我现在打开一个项目,里面有很多html文件,但是没有关联连接。所有现在我想在根目录下新建一个index.html文件,把这个项目下的所有html的路径都渲染到index.html的a标签下生成一个访问目录页面,需要看哪个静态页面我直接在index.html下点击就行了。这个要怎么玩?目前我的构建工具是用gulp,初玩gulp,不大懂 谢谢!

3 回复

深度截图20161109170413.png 深度截图20161109170352.png

没必要用gulp,自己动手丰衣足食:

const fs = require('fs')
const path = require('path')

function isDirectory (dir) {
  try {
    const stats = fs.statSync(dir)
    return stats.isDirectory()
  } catch (err) {
    return false
  }
}

function getFilenames (dir, regexp, recursive) {
  let files = []
  if (!isDirectory(dir)) throw new Error(dir + ' is not a directory!')
  fs.readdirSync(dir).forEach((filename) => {
    const fullPath = path.join(dir, filename)
    if (isDirectory(fullPath)) {
      if (recursive) files = files.concat(getFilenames(fullPath, regexp, recursive))
      else return
    }
    if (regexp && !regexp.test(filename)) return
    files.push(fullPath)
  })
  return files
}

getFilenames(__dirname, /.html$/, true).forEach((file) => {
  console.log(`<a href="${file}">${file}</a>`)
})

@varHarrie 谢谢啊,感激

@varHarrie 大神,请问我要怎么让他生成到html文件里去

回到顶部