最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filter - Add a BlockQuote at the beginning of a Para block when converting MD to HTML with Pandoc - Stack Overflow

programmeradmin1浏览0评论

I get the following error when I use a JavaScript Pandoc filter to add a BlockQuote:

Error in $.blocks[1]: When parsing Text.Pandoc.Definition.Block expected an Object with a tag field where the value is one of [Plain, Para, LineBlock, CodeBlock, RawBlock, BlockQuote, OrderedList, BulletList, DefinitionList, Header, HorizontalRule, Table, Figure, Div], but got Str.

I have a barebones MarkDown file with this content:

This is the first paragraph.

This is the second paragraph.

I want to use a Pandoc filter to insert a blockquote before the first paragraph, so that the HTML output looks like this:

<blockquote>
  <p>quoted block</p>
</blockquote>
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>

Here is my command:

pandoc -o output.html --filter=addBlockQuote.js input.md

Here is my addBlockQuote.js file, which also logs the incoming AST blocks to a filter.log file:

#!/usr/bin/env node

const pandoc = require("pandoc-filter")

pandoc.stdio(action);

let item = 0
let para = 0


function action({t: type, c: content}, format, meta) {
  log(arguments)

  switch (type) {
    case "Para":
      return prependBlockQuote(content)  
  }
}


function prependBlockQuote(content) {
  if (!para++) {
    const blockquote = {
      t: 'BlockQuote',
      c: [{ t: 'Para', c: [{ t: "Str", c: "quoted block"}] }]
    }

    // Prepend the blockquote to the Para block's content
    content.unshift(blockquote)
    return content
  }
}

// LOGGING //

const { createWriteStream } = require('fs')
const logger = new console.Console(createWriteStream("filter.log"));

function log(args) {
  const [{t: type, c: content}, format, meta] = args

  if (!item++) {
    logger.log(`format: ${format}

--- <<< meta ---
${JSON.stringify(meta, null, 2)}
--- meta >>> ---
`
    )
  }

  logger.log(`--- ${item} ---
type:   "${type}"`)
  if (content) {
    logger.log(`content: ${JSON.stringify(content, null, 2)}`)
  }
  logger.log("")
}

Here are the various AST blocks that are logged. You'll see that immediately after the first Para is logged, a Para with the "Str" text "quoted block" is logged, but no BlockQuote is shown. What do I need to change to get the BlockQuote to be treated?

format: html

--- <<< meta ---
{}
--- meta >>> ---

--- 1 ---
type:   "Para"
content: [
  {
    "t": "Str",
    "c": "This"
  },
  {
    "t": "Space"
  },
  {
    "t": "Str",
    "c": "is"
  },
  {
    "t": "Space"
  },
  {
    "t": "Str",
    "c": "the"
  },
  {
    "t": "Space"
  },
  {
    "t": "Str",
    "c": "first"
  },
  {
    "t": "Space"
  },
  {
    "t": "Str",
    "c": "paragraph."
  }
]

--- 2 ---
type:   "Para"
content: [
  {
    "t": "Str",
    "c": "quoted block"
  }
]

--- 3 ---
type:   "Str"
content: "quoted block"

--- 4 ---
type:   "Para"
content: [
  {
    "t": "Str",
    "c": "This"
  },
  {
    "t": "Space"
  },
  {
    "t": "Str",
    "c": "is"
  },
  {
    "t": "Space"
  },
  {
    "t": "Str",
    "c": "the"
  },
  {
    "t": "Space"
  },
  {
    "t": "Str",
    "c": "second"
  },
  {
    "t": "Space"
  },
  {
    "t": "Str",
    "c": "paragraph."
  }
]

--- 5 ---
type:   "Str"
content: "This"

--- 6 ---
type:   "Space"

--- 7 ---
type:   "Str"
content: "is"

--- 8 ---
type:   "Space"

--- 9 ---
type:   "Str"
content: "the"

--- 10 ---
type:   "Space"

--- 11 ---
type:   "Str"
content: "second"

--- 12 ---
type:   "Space"

--- 13 ---
type:   "Str"
content: "paragraph."

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论