0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

17_zibunyo

Last updated at Posted at 2023-07-03
const { execSync } = require('child_process');
const AWS = require('aws-sdk');
const fs = require('fs');

// Pandocのバイナリパス
const pandocBinary = '/opt/bin/pandoc'; // バイナリのパスに修正してください

exports.handler = async (event, context) => {
  const s3Client = new AWS.S3();

  // S3バケットとファイル情報を取得
  const record = event.Records[0];
  const inputBucket = record.s3.bucket.name;
  const inputKey = record.s3.object.key;
  const outputBucket = 's3-nodejs-pandoc-bucket'; // 変換後のHTMLファイルの出力先バケット名
  const outputKey = 'output.html'; // 変換後のHTMLファイルのキー

  // Markdownファイルを一時的なローカルファイルにダウンロード
  const tempInputFile = '/tmp/input.yaml';
  const downloadParams = {
    Bucket: inputBucket,
    Key: inputKey,
  };

  try {
    const data = await s3Client.getObject(downloadParams).promise();
    fs.writeFileSync(tempInputFile, data.Body);

    // Pandocのコマンドを構築
    const tempOutputFile = '/tmp/output.html';
    const pandocCommand = [
      pandocBinary,
      '-t', 'html',
      '-o', tempOutputFile,
      tempInputFile,
    ];

    // Pandocを実行
    execSync(pandocCommand.join(' '));

    // 変換後のHTMLファイルをS3にアップロード
    const uploadParams = {
      Bucket: outputBucket,
      Key: outputKey,
      Body: fs.readFileSync(tempOutputFile),
    };

    await s3Client.upload(uploadParams).promise();
    console.log('Conversion completed successfully.');
    return 'Conversion completed successfully.';
  } catch (error) {
    console.error('Conversion failed. Error:', error);
    throw error;
  }
};
title: "Sample Document"
author: "John Doe"
date: "2023-07-24"
content: |
  This is a sample document written in **Markdown** format.

  # aaa
  
  ## bbb

  # Heading 1
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.

  ## Heading 2
  Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

  ### Heading 3
  Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
wget https://github.com/jgm/pandoc/releases/download/2.17.1.1/pandoc-2.17.1.1-linux-amd64.tar.gz
tar -xzvf pandoc-2.17.1.1-linux-amd64.tar.gz
zip -r pandoc-layer.zip ./bin
const AWS = require('aws-sdk');
const fs = require('fs');

// Pandocのバイナリパス
const pandocBinary = '/opt/bin/pandoc'; // バイナリのパスに修正してください

exports.handler = async (event, context) => {
  const s3Client = new AWS.S3();

  // S3バケットとファイル情報を取得
  const record = event.Records[0];
  const inputBucket = record.s3.bucket.name;
  const inputKey = record.s3.object.key;
  const templateKey = 'templates/base_template.html';
  const tempInputFile = '/tmp/input.md';
  const tempTemplateFile = '/tmp/template.html';

  try {
    // Markdownファイルを一時的なローカルファイルにダウンロード
    const downloadInputParams = {
      Bucket: inputBucket,
      Key: inputKey,
    };
    await s3Client.download(downloadInputParams).promise();
    fs.renameSync(inputKey, tempInputFile);

    // テンプレートファイルを一時的なローカルファイルにダウンロード
    const downloadTemplateParams = {
      Bucket: inputBucket,
      Key: templateKey,
    };
    await s3Client.download(downloadTemplateParams).promise();
    fs.renameSync(templateKey, tempTemplateFile);

    // Pandocのコマンドを構築
    const tempOutputFile = '/tmp/output.html';
    const pandocCommand = [
      pandocBinary,
      '-f', 'markdown',
      '--template', tempTemplateFile, // テンプレートファイルを指定
      '-o', tempOutputFile,
      tempInputFile,
    ];

    // Pandocを実行
    const { status, stdout, stderr } = await execShellCommand(pandocCommand.join(' '));

    if (status === 0) {
      // 変換後のHTMLファイルをS3にアップロード
      const outputBucket = 's3-lambda-pandoc'; // 変換後のHTMLファイルの出力先バケット名
      const outputKey = 'output.html'; // 変換後のHTMLファイルのキー
      const uploadParams = {
        Bucket: outputBucket,
        Key: outputKey,
        Body: fs.readFileSync(tempOutputFile),
      };
      await s3Client.upload(uploadParams).promise();
      console.log('Conversion completed successfully.');
      return 'Conversion completed successfully.';
    } else {
      console.error('Conversion failed. Error:', stderr);
      throw new Error('Conversion failed.');
    }
  } catch (error) {
    console.error('An error occurred:', error);
    throw error;
  }
};

function execShellCommand(cmd) {
  const { exec } = require('child_process');
  return new Promise((resolve, reject) => {
    exec(cmd, (error, stdout, stderr) => {
      if (error) {
        reject(error);
      }
      resolve({ status: 0, stdout, stderr });
    });
  });
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?