Skip to content

use custom pandoc writer to preserve raw html blocks #553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.116.0 (Not yet released)

- Fix issue with raw html blocks being removed from document by Visual Editor (<https://github.com/quarto-dev/quarto/issues/552>)

## 1.115.0 (Release on 20 September 2024)

- Suppress background highlight on first line (<https://github.com/quarto-dev/quarto/pull/537>).
Expand Down
2 changes: 1 addition & 1 deletion apps/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"pandoc",
"quarto"
],
"version": "1.115.0",
"version": "1.116.0",
"repository": {
"type": "git",
"url": "https://github.com/quarto-dev/quarto/tree/main/apps/vscode"
Expand Down
23 changes: 23 additions & 0 deletions packages/editor-server/src/resources/md-writer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---@diagnostic disable: undefined-global
---
--- Writer for markdown that preserves raw html attributes.
---
--- This changed in pandoc 3.2, see https://github.com/rstudio/rstudio/issues/15189.

local html_formats = pandoc.List{'html', 'html4', 'html5'}
function Writer (doc, opts)
if opts.extensions:includes 'raw_attribute' then
doc = doc:walk{
RawBlock = function (raw)
if html_formats:includes(raw.format) then
local md = pandoc.write(pandoc.Pandoc(raw), 'markdown-raw_html')
return pandoc.RawBlock('markdown', md)
end
end
}
end
return pandoc.write(doc, {format = 'markdown', extensions = opts.extensions}, opts)
end

Extensions = pandoc.format.extensions 'markdown'
Template = pandoc.template.default 'markdown'
11 changes: 9 additions & 2 deletions packages/editor-server/src/server/pandoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ import { EditorServerOptions } from './server';

export function pandocServer(options: EditorServerOptions) : PandocServer {

// work around change in pandoc 3.2+: https://github.com/jgm/pandoc/issues/9677 by
// using a custom pandoc writer
function substituteCustomMarkdownWriter(format: string): string {
const customWriterScript = path.join(options.pandoc.resourcesDir, 'md-writer.lua')
return format.replace(/^markdown(?=[-+]|$)/, customWriterScript);
}

async function runPandoc(args: readonly string[] | null, stdin?: string) : Promise<string> {
return pandoc(options.pandoc, args, stdin);
}
Expand Down Expand Up @@ -100,9 +107,9 @@ export function pandocServer(options: EditorServerOptions) : PandocServer {
return ast;
},
async astToMarkdown(ast: PandocAst, format: string, options: string[]): Promise<string> {
const markdown = await runPandoc(
const markdown = await runPandoc(
["--from", "json",
"--to", format, ...options],
"--to", substituteCustomMarkdownWriter(format), ...options],
JSON.stringify(ast)
);
return markdown;
Expand Down