Skip to content

Commit

Permalink
Build
Browse files Browse the repository at this point in the history
  • Loading branch information
evanplaice committed Jul 3, 2020
1 parent 29073e2 commit a0be5b1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
46 changes: 43 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1686,9 +1686,18 @@ class WCMarkdown extends HTMLElement {
}
async connectedCallback() {
this.style.display = "block";
if (this.textContent) {
this.__value = this.textContent;
this.setValue();
const scriptTag = this.getElementsByTagName("script")[0];
if (scriptTag) {
if (scriptTag.getAttribute("type") === "wc-content") {
let content = WCMarkdown.dedentText(scriptTag.innerHTML);
content = content.replace(/&lt;(\/?script)(.*?)&gt;/g, "<$1$2>");
this.value = content;
}
} else {
if (this.textContent) {
this.__value = this.textContent;
this.setValue();
}
}
}
async setSrc(src) {
Expand Down Expand Up @@ -1720,6 +1729,37 @@ class WCMarkdown extends HTMLElement {
static highlight(element) {
prism.default.highlightAllUnder(element);
}
static dedentText(text) {
const lines = text.split("\n");
if (lines[0] === "")
lines.splice(0, 1);
const initline = lines[0];
let fwdPad = 0;
const usingTabs = initline[0] === " ";
const checkChar = usingTabs ? " " : " ";
while (true) {
if (initline[fwdPad] === checkChar) {
fwdPad += 1;
} else {
break;
}
}
const fixedLines = [];
for (const line of lines) {
let fixedLine = line;
for (let i = 0; i < fwdPad; i++) {
if (fixedLine[0] === checkChar) {
fixedLine = fixedLine.substring(1);
} else {
break;
}
}
fixedLines.push(fixedLine);
}
if (fixedLines[fixedLines.length - 1] === "")
fixedLines.splice(fixedLines.length - 1, 1);
return fixedLines.join("\n");
}
}
customElements.define("wc-markdown", WCMarkdown);
export {
Expand Down
Loading

0 comments on commit a0be5b1

Please sign in to comment.