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

Bugfix - If a header precedes a section, move it inside the secton #36

Closed
wants to merge 3 commits into from
Closed
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
94 changes: 55 additions & 39 deletions app/components/guides-article.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,93 @@
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import Component from "@ember/component";
import { inject as service } from "@ember/service";
import { computed } from "@ember/object";

function wrap(el, wrapper) {
el.parentNode.insertBefore(wrapper, el);
wrapper.appendChild(el);
el.parentNode.insertBefore(wrapper, el);
wrapper.appendChild(el);
}

export default Component.extend({
tagName: 'article',
classNames: 'chapter',
tagName: "article",
classNames: "chapter",
page: service(),
guidemaker: service(),
editVersion: computed('version', 'currentVersion', function() {
if(this.page.currentVersion === 'release') {
return '';
editVersion: computed("version", "currentVersion", function () {
if (this.page.currentVersion === "release") {
return "";
}

if(this.version === this.currentVersion) {
return 'release/'
if (this.version === this.currentVersion) {
return "release/";
}

return `${this.page.currentVersion}/`;
}),

didRender() {
let nodeList = this.element.querySelectorAll('pre:not(.no-line-numbers) > code');
let nodeList = this.element.querySelectorAll(
"pre:not(.no-line-numbers) > code"
);

if (nodeList) {
nodeList.forEach((code) => {
code.parentNode.classList.add('line-numbers');
code.parentNode.classList.add("line-numbers");
});
}

let filenameNodeList = this.element.querySelectorAll('pre > code[data-filename]');
let filenameNodeList = this.element.querySelectorAll(
"pre > code[data-filename]"
);

if (filenameNodeList) {
filenameNodeList.forEach((code) => {
if (code.parentNode.parentNode.classList.contains('filename')) {
if (code.parentNode.parentNode.classList.contains("filename")) {
//do nothing
return;
}

let filename = code.attributes['data-filename'].value;
let filename = code.attributes["data-filename"].value;
let match = filename.match(/\.(\w+)$/);

let ext = '';
let ext = "";

if (match && match[1]) {
ext = match[1];
}

let wrapper = document.createElement('div');
let wrapper = document.createElement("div");
wrapper.className = `filename ${ext}`;

let filenameSpan = document.createElement('span');
filenameSpan.innerText = code.attributes['data-filename'].value;
let filenameSpan = document.createElement("span");
filenameSpan.innerText = code.attributes["data-filename"].value;

let ribbon = document.createElement('div');
ribbon.className = 'ribbon';
let ribbon = document.createElement("div");
ribbon.className = "ribbon";

wrap(code.parentNode, wrapper);
code.parentNode.parentNode.prepend(filenameSpan);
code.parentNode.parentNode.prepend(ribbon);
});
}

let allHeaders = document.querySelectorAll("h1, h2, h3, h4, h5, h6")
let allHeaders = document.querySelectorAll("h1, h2, h3, h4, h5, h6");

for (var element of allHeaders) {
if (element.id) {
element.className = 'anchorable-toc'
let link = document.createElement('a');
link.className = 'toc-anchor';
element.className = "anchorable-toc";
let link = document.createElement("a");
link.className = "toc-anchor";
link.href = `#${element.id}`;
element.insertBefore(link, element.firstElementChild);
}

// If a heading precedes a section, move it inside that section
// This is important for accessibility.
let sibling = element.nextElementSibling;
if (sibling && sibling.nodeName === "SECTION") {
let header = element.parentElement.removeChild(element);
sibling.prepend(header);
}
}

Prism.highlightAll();
Expand All @@ -93,27 +105,31 @@ export default Component.extend({
*
**/
filenameNodeList.forEach((codeBlock) => {

let diffInfo = codeBlock.attributes['data-diff'] ? codeBlock.attributes["data-diff"].value.split(',') : [];
let diffInfo = codeBlock.attributes["data-diff"]
? codeBlock.attributes["data-diff"].value.split(",")
: [];

if (diffInfo.length === 0) {
return;
}

let lines = codeBlock.innerHTML.split('\n');
let lines = codeBlock.innerHTML.split("\n");

diffInfo.forEach(pD => {
diffInfo.forEach((pD) => {
let operator = pD[0];
let lineNo = +(pD.replace(operator, ''));
let lineNo = +pD.replace(operator, "");
let text = lines[lineNo - 1];
if (operator === '+') {
lines[lineNo - 1] = `<span class="diff-insertion"><span class="diff-operator">+</span>${text}</span>`;
if (operator === "+") {
lines[
lineNo - 1
] = `<span class="diff-insertion"><span class="diff-operator">+</span>${text}</span>`;
} else {
lines[lineNo - 1] = `<span class="diff-deletion"><span class="diff-operator">-</span>${text}</span>`;
lines[
lineNo - 1
] = `<span class="diff-deletion"><span class="diff-operator">-</span>${text}</span>`;
}
});
codeBlock.innerHTML = lines.join('\n');
})

}
codeBlock.innerHTML = lines.join("\n");
});
},
});