get all markdown files in a folder in typescript

To get all markdown files in a folder using TypeScript, we can use the Node.js fs (file system) module along with some TypeScript syntax, like so:

index.ts
import * as fs from 'fs';

const folderPath = './markdown-files'; // change this to your folder path

fs.readdir(folderPath, (err, files) => {
  if (err) {
    console.error('Error reading directory:', err);
    return;
  }

  const markdownFiles = files.filter(file => file.endsWith('.md'));

  console.log(markdownFiles); // array of markdown file names in the folder
});
374 chars
15 lines

Here we first define the path to the folder we want to search for markdown files. Then we use fs.readdir() to read the files in the directory. The function we pass in as the second argument will be called with the error (if any) and an array of files in the directory.

We then filter the files array to only include those that end with the .md file extension. Finally, we log out the resulting array of markdown file names. You can modify this code to suit your needs, such as parsing the markdown files or performing some other operation on them.

gistlibby LogSnag