Skip to content
Draft
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
2 changes: 1 addition & 1 deletion src/core/data-include.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function fillWithText(el, data, { replace }) {
const { includeFormat } = el.dataset;
let fill = data;
if (includeFormat === "markdown") {
fill = markdownToHtml(fill);
fill = markdownToHtml(fill, { fromHTML: false });
}

if (includeFormat === "text") {
Expand Down
16 changes: 11 additions & 5 deletions src/core/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,21 @@ function normalizeIndent(text) {
/**
* @param {string} text
* @param {object} options
* @param {boolean} options.inline
* @param {boolean} [options.inline]
* @param {boolean} [options.fromHTML]
*/
export function markdownToHtml(text, options = { inline: false }) {
export function markdownToHtml(
text,
options = { inline: false, fromHTML: true }
) {
const normalizedLeftPad = normalizeIndent(text);
// As markdown is pulled from HTML, > and & are already escaped and
// so blockquotes aren't picked up by the parser. This fixes it.
const potentialMarkdown = normalizedLeftPad
.replace(gtEntity, ">")
.replace(ampEntity, "&");
// When text comes from a raw fetch (data-include), skip this step.
const potentialMarkdown =

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot can you add a test?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test in tests/spec/core/data-include-spec.js (commit test(core/data-include): add test for entity unescape fix in raw markdown).

The test covers the regression case: &gt; in raw data-include markdown must not be converted to > (which would incorrectly produce a blockquote). It also verifies that a literal > in raw content still correctly becomes a <blockquote> element.

options.fromHTML !== false
? normalizedLeftPad.replace(gtEntity, ">").replace(ampEntity, "&")
: normalizedLeftPad;

const result = options.inline
? marked.parseInline(potentialMarkdown, config)
Expand Down
31 changes: 31 additions & 0 deletions tests/spec/core/data-include-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,37 @@ describe("Core — Data Include", () => {
expect(doc.getElementById("abstract")).toBeTruthy();
});

it("does not unescape HTML entities in raw markdown content", async () => {
// When markdown is fetched via data-include, it's raw text (not innerHTML).
// The &gt; entity must not be converted to >, or it would create a
// blockquote where the author intended literal &gt; text.
const includeBody = `
## Test

&gt; This is not a blockquote.

> This is a blockquote.
`;
const body = `<section
id="includes"
data-include-format="markdown"
data-include="${generateDataUrl(includeBody)}"
></section>`;

const ops = makeStandardOps(null, body);
const doc = await makeRSDoc(ops);

// &gt; in raw content must not become a blockquote
const p = doc.querySelector("#includes > p");
expect(p).toBeTruthy();
expect(p.textContent.trim()).toBe("> This is not a blockquote.");

// Literal > in raw content must still become a blockquote
const blockquote = doc.querySelector("#includes > blockquote");
expect(blockquote).toBeTruthy();
expect(blockquote.textContent.trim()).toBe("This is a blockquote.");
});

it("processes markdown with unescaped html code blocks", async () => {
const includeBody = `
## Test
Expand Down