Thanks for any time dedicated to this conundrum, in advance.
I am attempting to reprint the html markup of a button inside of a code element. However, I need to split the code elements up into separate spans, to add some color to the output.
Using Regex and HTML character encoding, I am able to correctly wrap all targeted element strings. This is where I hit the wall.
The out of the code is markup, indeed, but I need the wrapping spans to be only that… wrappers of a particular portion of the code string. I was thinking .split() .join(), but really do not want to deal with a huge array of substrings.
<span class="chev"><<span>button id=<span class="quote">">"<span>btn<span class="quote">">"<span> class=<span class="quote">">"<span>btn btn-primary tru-btn<span class="quote">">"<span> data-value=<span class="quote">">"<span>btn-primary<span class="quote">">"<span> type=<span class="quote">">"<span>button<span class="quote">">"<span><span class="chev">><span>Button<span class="chev"><<span>/butt
on<span class="chev">><span>
generateHTMLBlock = () => {
let someButton = document.querySelector('div').innerHTML,
modBtnCode = someButton.replace(/^.s+|s+$/gm, ''),
btnMarkup = modBtnCode.replace(/(.{100})/g, "$1r");
encodeElement = (output) => {
return output.replace(/</g, '<span class="chev"><<span>')
.replace(/>/g, '<span class="chev">><span>')
.replace(/"/g, '<span class="quote">">"<span>');
};
let code = encodeElement(btnMarkup);
document.getElementsByTagName('code').appendChild(code);
};
.btn-primary:not(disabled):not(.disabled),
.btn-secondary:not(disabled):not(.disabled) {
box-sizing: border-box;
display: flex;
align-items: center;
max-width: fit-content;
padding: 0.338rem 1.5rem;
margin: 0;
overflow: hidden;
background: black;
border-radius: .625rem;
border-width: .125rem;
border: 1px solid transparent;
border-color: black;
box-shadow: none;
font-family: arial;
color: white;
font-size: 16px;
font-weight: 400;
text-align: center;
vertical-align: middle;
text-transform: none;
user-select: none;
line-height: 2.2rem;
letter-spacing: .25px;
text-decoration: none !important;
cursor: pointer;
transition: all .3s ease-in-out;
}
pre {
padding: 1rem;
margin-bottom: 0;
width: 100%;
height: 6em;
}
code {
display: block;
color: black;
width: 100%;
height: 100%;
border: 1px solid red;
}
.quote {
color: green;
}
.chev {
color: blue;
}
<div>
<button id="btn" class="btn btn-primary" data-value="btn-primary" type="button">Button</button>
</div>
<pre>
<code></code>
</pre>