Skip to content
Open
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
724 changes: 362 additions & 362 deletions builds/respec-w3c.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion builds/respec-w3c.js.map

Large diffs are not rendered by default.

28 changes: 26 additions & 2 deletions src/core/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
*/

import { getElementIndentation, reindent } from "./utils.js";
import { getElementIndentation } from "./utils.js";
import { marked } from "./import-maps.js";

export const name = "core/markdown";
Expand Down Expand Up @@ -92,13 +92,37 @@ const config = {
renderer: /** @type {any} */ (new Renderer()),
};

/**
* Normalize indentation of text by stripping the leading whitespace determined
* from the first non-empty line. This handles mixed-indentation HTML content
* where some lines (e.g., from rendered markdown inside sections) may have less
* indentation than the outer HTML structure.
* @param {string} text
*/
function normalizeIndent(text) {
if (!text) return text;
const lines = text.trimEnd().split("\n");
while (lines.length && !lines[0].trim()) {
lines.shift();
}
if (!lines.length) return "";
// After the while loop, lines[0] is guaranteed to have non-whitespace content,
// so search() will return a non-negative index (0 means no leading whitespace).
const firstIndent = lines[0].search(/[^\s]/);
if (firstIndent < 1) return lines.join("\n");
const prefix = " ".repeat(firstIndent);
return lines
.map(s => (s.startsWith(prefix) ? s.slice(firstIndent) : s))
.join("\n");
}

/**
* @param {string} text
* @param {object} options
* @param {boolean} options.inline
*/
export function markdownToHtml(text, options = { inline: false }) {
const normalizedLeftPad = reindent(text);
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
Expand Down
Loading