VFile について

2022-12-04T08:25

to-vfileVFile を扱うモジュールです。

import { read, write, toVFile } from 'to-vfile';

remark と相性がいいっていうか、同じ作者さんが作ってるみたいなので、remark の例によく出てきます。

read() は実のところ、Perl における slurp のようなものです。
違うのは string ではなくて VFile を返すところです。

const vfile /* : VFile */ = await read('./hoge.md');

.value でファイルの内容を参照できます。

console.log(vfile.value); // => <ファイルの内容>

write() がほんとにわかりませんでしたが、test.js を見て理解することができました:

const destFilePath = changeExtention('./hoge.md', '.html');
await write({ path: destFilePath, data: 'Hello, World!' });

NodeJS の fs.writeFile() 風に書くと以下のようになります:

async function writeVFile(filepath, data /*, options */) {
  const vfile = {
    path: filepath,
    data: data
  };

  await write(vfile);
}

うぃーくりー・あんだーわーるど・にゅーす

テクノロジー、サイエンス、そして不思議な出来事についてのブログです。

Quick Links

Follow Us

© 2025 うぃーくりー・あんだーわーるど・にゅーす. All rights reserved.