Skip to content

Commit

Permalink
If a heading precedes a section, move it inside that section
Browse files Browse the repository at this point in the history
  • Loading branch information
jenweber committed Jun 3, 2022
1 parent 4d3e8a5 commit 75c0f30
Showing 1 changed file with 47 additions and 39 deletions.
86 changes: 47 additions & 39 deletions app/components/guides-article.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,82 @@
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);
}
Expand Down Expand Up @@ -101,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");
});
},
});

0 comments on commit 75c0f30

Please sign in to comment.