Skip to content
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

feat(core/markdown): add data-format support #1216

Merged
merged 2 commits into from
May 15, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 41 additions & 8 deletions src/core/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ function toHTML(text) {
const potentialMarkdown = normalizedLeftPad
.replace(/>/gm, ">")
.replace(/&/gm, "&");
return marked(potentialMarkdown);
const result = marked(potentialMarkdown);
return result;
}

function processElements(selector) {
return element => {
Array.from(element.querySelectorAll(selector))
.reverse()
.forEach(element => {
element.innerHTML = toHTML(element.innerHTML);
});
const elements = Array.from(element.querySelectorAll(selector));
elements.reverse().forEach(element => {
element.innerHTML = toHTML(element.innerHTML);
});
return elements;
};
}

Expand Down Expand Up @@ -175,12 +176,44 @@ function substituteWithTextNodes(elements) {
});
}

const processMDSections = processElements("[data-format='markdown']:not(body)");
const processBlockLevelElements = processElements(
"section, div, address, article, aside, figure, header, main, body"
"[data-format=markdown]:not(body), section, div, address, article, aside, figure, header, main, body"
);

export function run(conf, doc, cb) {
if (conf.format !== "markdown") {
const hasMDSections = !!doc.querySelector("[data-format=markdown]:not(body)");
const isMDFormat = conf.format === "markdown";
if (!isMDFormat && !hasMDSections) {
return cb(); // Nothing to be done
}
// Only has markdown-format sections
if (!isMDFormat) {
processMDSections(doc.body)
.map(elem => {
const structuredInternals = structure(elem, elem.ownerDocument);
return {
structuredInternals,
elem,
};
})
.forEach(({ elem, structuredInternals }) => {
elem.setAttribute("aria-busy", "true");
if (
structuredInternals.firstElementChild.localName === "section" &&
elem.localName === "section"
) {
const section = structuredInternals.firstElementChild;
section.remove();
while (section.hasChildNodes()) {
elem.appendChild(section.firstChild);
}
} else {
elem.innerHTML = "";
}
elem.appendChild(structuredInternals);
elem.setAttribute("aria-busy", "false");
});
return cb();
}
// We transplant the UI to do the markdown processing
Expand Down
1 change: 1 addition & 0 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export function normalizePadding(text) {
doc.normalize();
// use the first space as an indicator of how much to chop off the front
const firstSpace = doc.body.innerText
.replace(/^\ *\n/, "")
.split("\n")
.filter(item => item && item.startsWith(" "))[0];
var chop = firstSpace ? firstSpace.match(/\ +/)[0].length : 0;
Expand Down
38 changes: 38 additions & 0 deletions tests/spec/core/markdown-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,42 @@ describe("Core - Markdown", function() {
}).then(done);
});
});
describe("data-format=markdown", () => {
it("replaces processes data-format=markdown sections, but leaves other sections alone", done => {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
`
<section id=markdown1 data-format=markdown>
## this is a h2
This is a paragraph with \`code\`.

### heading 3
This is another paragraph.

### another h3
This is another paragraph.
</section>
<section id=dontTouch>
## this should not change
</section>
`,
};
ops.config.doRDFa = false;
makeRSDoc(ops, doc => {
const headings = Array.from(
doc.querySelectorAll("#markdown1 h2, #markdown1 h3")
);
expect(headings.length).toEqual(3);
const [h2, h3, anotherH3] = headings;
expect(h2.localName).toEqual("h2");
expect(h3.localName).toEqual("h3");
expect(anotherH3.localName).toEqual("h3");
expect(anotherH3.textContent.trim()).toEqual("1.2 another h3");
expect(doc.querySelector("#markdown1 code")).toBeTruthy();
const dontChange = doc.querySelector("#dontTouch").textContent.trim();
expect(dontChange).toEqual("## this should not change");
}).then(done);
});
});
});