Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion asset/css/markbind.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ code {
word-break: normal;
}

pre {
position: relative;
}

pre.hljs > code {
background: none;
}
Expand All @@ -19,7 +23,6 @@ pre > code.hljs {
-webkit-background-clip: padding-box; /* for Safari */
background: ghostwhite;
background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */
border: 1px solid rgb(200, 200, 200);
border: 1px solid rgba(200, 200, 200, 0.3);
border-radius: 5px;
}
Expand Down Expand Up @@ -52,6 +55,26 @@ kbd {
color: #555;
}

pre .clipboard-icon {
position: absolute;
top: 4px;
z-index: 2;
cursor: pointer;
border: 1px solid transparent;
color: #ccc;
background-color: transparent;
height: 30px;
transition: all .25s ease-out;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the attributes should be ordered alphabetically for CSS 🙂


pre .clipboard-icon:hover {
color: #555;
}

pre .clipboard-btn {
right: 10px;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems too much, it's noticeably further from the right than the top.

}

code.hljs.inline {
background: #f8f8f8;
color: #333;
Expand Down
7 changes: 7 additions & 0 deletions asset/js/clipboard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions asset/js/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,30 @@ function setupAnchors() {
});
}

function setupCopyButtonsForCodeBlocks() {
const clipboard = new ClipboardJS('.clipboard-btn', {
target(trigger) {
return trigger.parentNode.querySelector('code');
},
});

clipboard.on('success', (e) => {
const clipboardBtn = jQuery(e.trigger);
clipboardBtn.removeClass('fa fa-clipboard');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copy icon seems more appropriate.

clipboardBtn.addClass('fa fa-clipboard-check');
e.clearSelection();
setTimeout(() => {
clipboardBtn.removeClass('fa fa-clipboard-check');
clipboardBtn.addClass('fa fa-clipboard');
}, 2000);
});
}

function executeAfterMountedRoutines() {
flattenModals();
scrollToUrlAnchorHeading();
setupAnchors();
setupCopyButtonsForCodeBlocks();
}

function setupSiteNav() {
Expand Down
15 changes: 15 additions & 0 deletions src/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const SITE_NAV_ID = 'site-nav';
const TITLE_PREFIX_SEPARATOR = ' - ';

const ANCHOR_HTML = '<a class="fa fa-anchor" href="#"></a>';
const COPY_BUTTON_HTML = '<button class="fa fa-clipboard clipboard-icon clipboard-btn"></button>';
const DROPDOWN_BUTTON_ICON_HTML = '<i class="dropdown-btn-icon">\n'
+ '<span class="glyphicon glyphicon-menu-down" aria-hidden="true"></span>\n'
+ '</i>';
Expand Down Expand Up @@ -469,6 +470,19 @@ Page.prototype.addAnchors = function (content) {
return $.html();
};

/**
* Adds copy buttons to code blocks in the page
* @param content of the page
*/
Page.prototype.addCopyButtons = function (content) {
const $ = cheerio.load(content, { xmlMode: false });
const codeBlockSelector = 'pre';
$(codeBlockSelector).each((i, codeBlock) => {
$(codeBlock).append(COPY_BUTTON_HTML);
});
return $.html();
};

/**
* Records the dynamic or static included files into this.includedFiles
* @param dependencies array of maps of the external dependency and where it is included
Expand Down Expand Up @@ -767,6 +781,7 @@ Page.prototype.generate = function (builtFiles) {
.then(() => markbinder.renderFile(this.tempPath, fileConfig))
.then(result => this.filterTags(this.frontMatter.tags, result))
.then(result => this.addAnchors(result))
.then(result => this.addCopyButtons(result))
.then((result) => {
this.content = htmlBeautify(result, { indent_size: 2 });

Expand Down
2 changes: 2 additions & 0 deletions src/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ Site.prototype.createPage = function (config) {
path.join(this.siteAssetsDestPath, 'js', 'bootstrap-vue.min.js')),
polyfillJs: path.relative(path.dirname(resultPath),
path.join(this.siteAssetsDestPath, 'js', 'polyfill.min.js')),
clipboardJs: path.relative(path.dirname(resultPath),
path.join(this.siteAssetsDestPath, 'js', 'clipboard.min.js')),
setup: path.relative(path.dirname(resultPath),
path.join(this.siteAssetsDestPath, 'js', 'setup.js')),
vue: path.relative(path.dirname(resultPath),
Expand Down
1 change: 1 addition & 0 deletions src/template/page.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<script src="<%- asset.bootstrapUtilityJs %>"></script>
<script src="<%- asset.polyfillJs %>"></script>
<script src="<%- asset.bootstrapVueJs %>"></script>
<script src="<%- asset.clipboardJs %>"></script>
<script>
const baseUrl = '<%- baseUrl %>'
</script>
Expand Down
1 change: 1 addition & 0 deletions test/test_site/expected/bugs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ <h2 id="remove-extra-space-in-links">Remove extra space in links<a class="fa fa-
<script src="..\markbind\js\bootstrap-utility.min.js"></script>
<script src="..\markbind\js\polyfill.min.js"></script>
<script src="..\markbind\js\bootstrap-vue.min.js"></script>
<script src="..\markbind\js\clipboard.min.js"></script>
<script>
const baseUrl = '/test_site'
</script>
Expand Down
1 change: 1 addition & 0 deletions test/test_site/expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ <h1 id="heading-in-footer-should-not-be-indexed">Heading in footer should not be
<script src="markbind\js\bootstrap-utility.min.js"></script>
<script src="markbind\js\polyfill.min.js"></script>
<script src="markbind\js\bootstrap-vue.min.js"></script>
<script src="markbind\js\clipboard.min.js"></script>
<script>
const baseUrl = '/test_site'
</script>
Expand Down
24 changes: 24 additions & 0 deletions test/test_site/expected/markbind/css/markbind.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ code {
word-break: normal;
}

pre {
position: relative;
}

pre.hljs > code {
background: none;
}
Expand Down Expand Up @@ -52,6 +56,26 @@ kbd {
color: #555;
}

pre .clipboard-icon {
position: absolute;
top: 4px;
z-index: 2;
cursor: pointer;
border: 1px solid transparent;
color: #ccc;
background-color: transparent;
height: 30px;
transition: all .25s ease-out;
}

pre .clipboard-icon:hover {
color: #555;
}

pre .clipboard-btn {
right: 10px;
}

code.hljs.inline {
background: #f8f8f8;
color: #333;
Expand Down
Loading