Skip to content

Commit 631bef2

Browse files
committed
feat: replace markdown-style layout with div and css structure. #97
1 parent a9d4f26 commit 631bef2

File tree

7 files changed

+99
-217
lines changed

7 files changed

+99
-217
lines changed

packages/action/dist/action.js

Lines changed: 18 additions & 48 deletions
Large diffs are not rendered by default.

packages/cli/src/create.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { githubCorners } from './nodes/github-corners.js';
1111
import { githubCornersFork } from './nodes/github-corners-fork.js';
1212
import { octiconLink } from './nodes/octiconLink.js';
1313
import { imgBase64 as toBase64 } from './nodes/imgBase64.js';
14-
import { markdownStyle } from './nodes/markdown-style.js';
15-
import { copyElement, copyStyle, copyScript } from './nodes/copy.js';
14+
import { markdownStyle, mdStyle } from './nodes/markdown-style.js';
15+
import { copyElement, copyScript } from './nodes/copy.js';
1616
import { darkMode } from './nodes/dark-mode.js';
1717
import { MDToHTMLOptions } from './index.js';
1818

@@ -74,10 +74,6 @@ export function create(options: MDToHTMLOptions = {}) {
7474
child.children = [octiconLink()];
7575
}
7676
}
77-
if (node.type == 'element' && node.tagName === 'markdown-style') {
78-
node.children.push(copyStyle());
79-
node.children.push(copyScript());
80-
}
8177
if (node.type == 'element' && node.tagName === 'pre') {
8278
const code = getCodeString(node.children);
8379
node.children.push(copyElement(code));
@@ -106,6 +102,18 @@ export function create(options: MDToHTMLOptions = {}) {
106102
documentOptions.style = Array.isArray(document.style) ? document.style : [document.style];
107103
}
108104

105+
if (Array.isArray(documentOptions.style)) {
106+
documentOptions.style.push(mdStyle);
107+
} else if (typeof documentOptions.style === 'string') {
108+
documentOptions.style = [documentOptions.style, mdStyle];
109+
}
110+
documentOptions.script = documentOptions.script || [];
111+
if (Array.isArray(documentOptions.script)) {
112+
documentOptions.script.push(copyScript);
113+
} else if (typeof documentOptions.script === 'string') {
114+
documentOptions.script = [documentOptions.script, copyScript];
115+
}
116+
109117
mdOptions.rehypePlugins.unshift([rehypeDocument, documentOptions]);
110118
}
111119

packages/cli/src/nodes/copy.ts

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,6 @@
11
import { Element } from 'hast';
22

3-
const style = `markdown-style pre .copied {
4-
display: flex;
5-
position: absolute;
6-
cursor: pointer;
7-
color: #a5afbb;
8-
top: 6px;
9-
right: 6px;
10-
border-radius: 5px;
11-
background: #82828226;
12-
padding: 6px;
13-
font-size: 12px;
14-
transition: all .3s;
15-
}
16-
markdown-style pre .copied:not(.active) {
17-
visibility: hidden;
18-
}
19-
markdown-style pre:hover .copied {
20-
visibility: visible;
21-
}
22-
markdown-style pre:hover .copied:hover {
23-
background: #4caf50;
24-
color: #fff;
25-
}
26-
markdown-style pre:hover .copied:active,
27-
markdown-style pre .copied.active {
28-
background: #2e9b33;
29-
color: #fff;
30-
}
31-
markdown-style pre .copied .octicon-copy {
32-
display: block;
33-
}
34-
markdown-style pre .copied .octicon-check {
35-
display: none;
36-
}
37-
markdown-style pre .active .octicon-copy {
38-
display: none;
39-
}
40-
markdown-style pre .active .octicon-check {
41-
display: block;
42-
}`;
43-
44-
export function copyStyle(): Element {
45-
return {
46-
type: 'element',
47-
tagName: 'style',
48-
properties: {},
49-
children: [
50-
{
51-
type: 'text',
52-
value: style
53-
}
54-
]
55-
}
56-
}
57-
58-
const script = `/*! @uiw/copy-to-clipboard v1.0.12 | MIT (c) 2021 Kenny Wang | https://github.com/uiwjs/copy-to-clipboard.git */
3+
export const copyScript = `/*! @uiw/copy-to-clipboard v1.0.12 | MIT (c) 2021 Kenny Wang | https://github.com/uiwjs/copy-to-clipboard.git */
594
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).copyTextToClipboard=t()}(this,(function(){"use strict";return function(e,t){const o=document.createElement("textarea");o.value=e,o.setAttribute("readonly",""),o.style={position:"absolute",left:"-9999px"},document.body.appendChild(o);const n=document.getSelection().rangeCount>0&&document.getSelection().getRangeAt(0);o.select();let c=!1;try{c=!!document.execCommand("copy")}catch(e){c=!1}document.body.removeChild(o),n&&document.getSelection&&(document.getSelection().removeAllRanges(),document.getSelection().addRange(n)),t&&t(c)}}));
605
616
function copied(target, str) {
@@ -67,20 +12,6 @@ function copied(target, str) {
6712
});
6813
}`;
6914

70-
export function copyScript(): Element {
71-
return {
72-
type: 'element',
73-
tagName: 'script',
74-
properties: {},
75-
children: [
76-
{
77-
type: 'text',
78-
value: script
79-
}
80-
]
81-
}
82-
}
83-
8415
export function copyElement(str: string = ''): Element {
8516
return {
8617
type: 'element',

packages/cli/src/nodes/markdown-style.ts

Lines changed: 6 additions & 87 deletions
Large diffs are not rendered by default.

packages/cli/src/nodes/markdown.css

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/create.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ it('options.document.js=[] test case', async () => {
7272

7373
it('options=undefined test case', async () => {
7474
expect(create({ 'dark-mode': false }).indexOf('<dark-mode permanent') > -1).toBeFalsy();
75-
expect(create().indexOf('</markdown-style>') > -1).toBeTruthy();
7675
});
7776

7877
it('options.document=undefined test case', async () => {
@@ -88,7 +87,6 @@ it('github-corners test case', async () => {
8887
});
8988
expect(html.indexOf('<dark-mode style=') > 0).toBeTruthy();
9089
expect(html.indexOf('<github-corners target="__blank"') > 0).toBeTruthy();
91-
expect(html.indexOf('<markdown-style') > 0).toBeTruthy();
9290
expect(html.indexOf('max-width: 960px; margin: 0 auto 60px auto; padding: 8px') > 0).toBeTruthy();
9391
expect(html.indexOf('class="markdown-style"') > 0).toBeTruthy();
9492

test/index.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ it('copyElement test case', async () => {
2020
});
2121

2222
it('markdownStyle test case', async () => {
23-
console.log(`markdownStyle([], 'light')[1].properties::::`, markdownStyle([], 'light')[1].properties)
24-
expect(markdownStyle([], undefined).length).toEqual(2);
25-
expect(markdownStyle([], 'light')[1].properties).toEqual(expect.objectContaining({
23+
expect(markdownStyle([], undefined).length).toEqual(1);
24+
expect(markdownStyle([], 'light')[0].properties).toEqual(expect.objectContaining({
2625
"className": "markdown-style",
2726
"mode": "light",
2827
"style": "max-width: 960px; margin: 0 auto 60px auto; padding: 8px;",
2928
}));
30-
expect(markdownStyle([], 'dark')[1].properties).toEqual(expect.objectContaining({
29+
expect(markdownStyle([], 'dark')[0].properties).toEqual(expect.objectContaining({
3130
"className": "markdown-style",
3231
"mode": "dark",
3332
"style": "max-width: 960px; margin: 0 auto 60px auto; padding: 8px;",

0 commit comments

Comments
 (0)