Skip to content

Commit

Permalink
feat: add code block lnline-color feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 12, 2022
1 parent 295e3e6 commit 9094c4e
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 6 deletions.
10 changes: 6 additions & 4 deletions Sources/Markdown/Resources/web.bundle/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
<head>
<title>Markdown Preview</title>
<link rel="stylesheet" href="marked.css">
<script type="text/javascript" src="./prism.js"></script>
<script src="./marked.min.js" type="text/javascript" charset="utf-8"></script>
<script src="./copy.js" type="text/javascript"></script>
<link rel="stylesheet" href="plugin.css">
</head>
<body>
<div id="__markdown_preview__" class="markdown-body"></div>
<script src="./main.js" type="text/javascript"></script>
<script type="text/javascript" src="./prism.js"></script>
<script type="text/javascript" src="./plugins/lnline-color.js"></script>
<script type="text/javascript" src="./marked.min.js" charset="utf-8"></script>
<script type="text/javascript" src="./copy.js"></script>
<script type="text/javascript" src="./main.js"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions Sources/Markdown/Resources/web.bundle/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// marked.setOptions({
// highlight: (code, lang) => {
// const language = Prism.languages[lang];
// if (language) {
// if (lang && Prism.languages[lang]) {
// const language = Prism.languages[lang];
// return Prism.highlight(code, language, lang);
// }
// return code;
Expand Down
3 changes: 3 additions & 0 deletions Sources/Markdown/Resources/web.bundle/marked.css
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ body, html {
.token.rule, .token.regex, .token.important, .token.keyword {
color: var(--color-prettylights-syntax-keyword);
}
.token.coord {
color: var(--color-prettylights-syntax-meta-diff-range);
}
.token.important, .token.bold { font-weight: bold; }
.token.italic { font-style: italic; }
.token.entity { cursor: help; }
Expand Down
37 changes: 37 additions & 0 deletions Sources/Markdown/Resources/web.bundle/plugin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
span.inline-color-wrapper{
background: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyIDIiPjxwYXRoIGZpbGw9ImdyYXkiIGQ9Ik0wIDBoMnYySDB6Ii8+PHBhdGggZmlsbD0id2hpdGUiIGQ9Ik0wIDBoMXYxSDB6TTEgMWgxdjFIMXoiLz48L3N2Zz4=);
background-position:center;
background-size:110%;
display:inline-block;
height:1.333ch;
width:1.333ch;
margin:0 .333ch;
box-sizing:border-box;
border:1px solid #fff;
outline:1px solid rgba(0,0,0,.5);
overflow:hidden
}
span.inline-color{
display: inline-block;
height: 120%;
width: 120%;
position: relative;
top: -4px;
}


pre.language-diff > code .token.deleted,
pre > code.language-diff .token.deleted {
color: var(--color-prettylights-syntax-carriage-return-bg);
/* background-color:rgba(255,0,0,.1); */
/* background-color: var(--color-prettylights-syntax-comment); */
/* color:inherit;
display:block */
}
pre.language-diff > code .token.inserted,
pre > code.language-diff .token.inserted {
color: var(--color-prettylights-syntax-entity-tag);
/* background-color:rgba(0,255,128,.1); */
/* color:inherit;
display:block */
}
105 changes: 105 additions & 0 deletions Sources/Markdown/Resources/web.bundle/plugins/lnline-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
(function () {

if (typeof Prism === 'undefined' || typeof document === 'undefined') {
return;
}

// Copied from the markup language definition
var HTML_TAG = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/g;

// a regex to validate hexadecimal colors
var HEX_COLOR = /^#?((?:[\da-f]){3,4}|(?:[\da-f]{2}){3,4})$/i;

/**
* Parses the given hexadecimal representation and returns the parsed RGBA color.
*
* If the format of the given string is invalid, `undefined` will be returned.
* Valid formats are: `RGB`, `RGBA`, `RRGGBB`, and `RRGGBBAA`.
*
* Hexadecimal colors are parsed because they are not fully supported by older browsers, so converting them to
* `rgba` functions improves browser compatibility.
*
* @param {string} hex
* @returns {string | undefined}
*/
function parseHexColor(hex) {
var match = HEX_COLOR.exec(hex);
if (!match) {
return undefined;
}
hex = match[1]; // removes the leading "#"

// the width and number of channels
var channelWidth = hex.length >= 6 ? 2 : 1;
var channelCount = hex.length / channelWidth;

// the scale used to normalize 4bit and 8bit values
var scale = channelWidth == 1 ? 1 / 15 : 1 / 255;

// normalized RGBA channels
var channels = [];
for (var i = 0; i < channelCount; i++) {
var int = parseInt(hex.substr(i * channelWidth, channelWidth), 16);
channels.push(int * scale);
}
if (channelCount == 3) {
channels.push(1); // add alpha of 100%
}

// output
var rgb = channels.slice(0, 3).map(function (x) {
return String(Math.round(x * 255));
}).join(',');
var alpha = String(Number(channels[3].toFixed(3))); // easy way to round 3 decimal places

return 'rgba(' + rgb + ',' + alpha + ')';
}

/**
* Validates the given Color using the current browser's internal implementation.
*
* @param {string} color
* @returns {string | undefined}
*/
function validateColor(color) {
var s = new Option().style;
s.color = color;
return s.color ? color : undefined;
}

/**
* An array of function which parse a given string representation of a color.
*
* These parser serve as validators and as a layer of compatibility to support color formats which the browser
* might not support natively.
*
* @type {((value: string) => (string|undefined))[]}
*/
var parsers = [
parseHexColor,
validateColor
];


Prism.hooks.add('wrap', function (env) {
if (env.type === 'color' || env.classes.indexOf('color') >= 0) {
var content = env.content;

// remove all HTML tags inside
var rawText = content.split(HTML_TAG).join('');

var color;
for (var i = 0, l = parsers.length; i < l && !color; i++) {
color = parsers[i](rawText);
}

if (!color) {
return;
}

var previewElement = '<span class="inline-color-wrapper"><span class="inline-color" style="background-color:' + color + ';"></span></span>';
env.content = previewElement + content;
}
});

}());

0 comments on commit 9094c4e

Please sign in to comment.