import { readFileSync } from 'fs';
import { resolve } from 'path';

const parseArticle = (fileName) => {
  var _a;
  const articlesPath = resolve(process.env.VAXLA_CONFIG, "articles", fileName);
  const file = readFileSync(articlesPath);
  const fileString = file.toString("utf-8");
  const fileParts = fileString.split("---");
  let error = void 0;
  let document = "";
  let metadata = {};
  if (fileParts.length > 2) {
    const [_, metadataString, ...documentSplit] = fileParts;
    document = documentSplit.join("---");
    metadataString.split("\n").forEach((line) => {
      const [key, ...valueParts] = line.split(":");
      if (key && valueParts.length > 0) {
        metadata[key.trim()] = valueParts.join(":").trim();
      }
    });
  } else {
    const [...documentSplit] = fileParts;
    document = documentSplit.join("---");
  }
  const fileNameParts = fileName.split(".");
  let title = "";
  let uploadDate = 0;
  if (fileNameParts.length === 3) {
    const [uploadDateString, titleString, extension] = fileNameParts;
    uploadDate = new Date(uploadDateString).getTime();
    title = titleString;
    if (extension !== "md") error = "Please end your article file name with `.md`.";
  } else {
    title = fileName;
    const date = /* @__PURE__ */ new Date();
    error = `Please follow the format of \`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}\`.<title>.md`;
  }
  return {
    fileName,
    title: (_a = metadata["title"]) != null ? _a : title,
    metadata,
    document,
    uploadDate,
    error
  };
};

export { parseArticle };
//# sourceMappingURL=_shared.mjs.map
