Skip to content

Commit 17f899d

Browse files
committed
add view original source link to source block
1 parent 7fd77b1 commit 17f899d

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

gulp.d/tasks/build-preview-pages.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,15 @@ module.exports = (src, previewSrc, previewDest, sink = () => map()) => (done) =>
106106
pageModel.attributes = Object.entries({ ...attributes, ...componentVersion.asciidoc.attributes })
107107
.filter(([name, val]) => name.startsWith('page-'))
108108
.reduce((accum, [name, val]) => ({ ...accum, [name.substr(5)]: val }), {})
109-
pageModel.contents = Buffer.from(doc.convert())
109+
pageModel.contents = Buffer.from(
110+
doc
111+
.convert()
112+
// NOTE emulates the behavior of the view source url extension
113+
.replace(
114+
/<pre([^>]+)><code([^>]+)>\[data-source-url=(.+?)\]\n/g,
115+
'<pre$1><code$2 data-source-url="$3">'
116+
)
117+
)
110118
}
111119
file.extname = '.html'
112120
try {

preview-src/index.adoc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,17 @@ The *Server Resources* statistics section displays the resource information on t
275275
.A query using the fluent API (lines are forced to wrap)
276276
[source,java]
277277
----
278-
Observable<AsyncN1qlQueryResult> theAirline = bucket.async() // <1>
279-
.query(
280-
N1qlQuery.simple(select("name").from("travel_sample").as("Airline").where(x("id").eq(5209)))
281-
);
282-
return thisAirline; // <2>
278+
[data-source-url=https://github.com/couchbase/docs-sdk-java/blob/20c44c9e7ca86ea475b7aa33c58d5670aa7c4495/modules/ROOT/pages/n1ql-queries-with-sdk.adoc#L53-L58]
279+
Statement statement = select("fname", "lname", "age").from(i("default")).where(x("age").gt(x("$age"))); // <1>
280+
JsonObject placeholderValues = JsonObject.create().put("age", 22);
281+
q = N1qlQuery.parameterized(statement, placeholderValues); // <2>
282+
for (N1qlQueryRow row : bkt.query(q)) { // <3>
283+
System.out.println(row);
284+
}
283285
----
284-
<1> Sets up an async query.
285-
<2> Returns the result of the query.
286+
<1> Defines the query.
287+
<2> Inserts placeholder values into the query.
288+
<3> Runs the query.
286289

287290
[#vbucket-stats]
288291
== Monitoring `vBucket` Resources

src/css/clipboard.css

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
.view-source-button {
2+
position: absolute;
3+
right: 1rem;
4+
top: 12px;
5+
display: none;
6+
color: var(--color-brand-gray4);
7+
font-family: "Source Sans Pro", sans-serif;
8+
z-index: 1;
9+
}
10+
11+
.doc .listingblock:hover .view-source-button {
12+
display: block;
13+
}
14+
115
.copy-code-button {
216
position: absolute;
317
background: url(../img/copy.png) no-repeat center right/contain;
418
width: 24px;
519
height: 14px;
6-
right: 1rem;
20+
right: 3.2rem;
721
top: 12px;
822
display: none;
923
cursor: pointer;

src/css/doc.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@
586586
text-transform: uppercase;
587587
position: absolute;
588588
top: 12px;
589-
right: 2.4rem;
589+
right: 4.5rem;
590590
font-family: "Source Sans Pro", sans-serif;
591591
padding-right: 0.5rem;
592592
border-right: 1px solid var(--color-brand-gray9);

src/js/07-copy-to-clipboard.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
;(function () {
22
'use strict'
33
document.querySelectorAll('pre > code').forEach(function (codeBlock) {
4-
var button = document.createElement('a')
5-
button.className = 'copy-code-button'
6-
//button.type = 'button'
7-
button.dataset.title = 'Copy'
4+
var viewSourceLink
5+
var sourceUrl = codeBlock.dataset.sourceUrl
6+
if (sourceUrl) {
7+
viewSourceLink = document.createElement('a')
8+
viewSourceLink.href = codeBlock.dataset.sourceUrl
9+
viewSourceLink.className = 'view-source-button'
10+
viewSourceLink.target = '_blank'
11+
viewSourceLink.dataset.title = 'View Source'
12+
viewSourceLink.appendChild(document.createTextNode('View'))
13+
}
14+
15+
var copyButton = document.createElement('a')
16+
copyButton.className = 'copy-code-button'
17+
//copyButton.type = 'button'
18+
copyButton.dataset.title = 'Copy'
819

920
var dataSource = document.createElement('span')
1021
dataSource.className = 'data-source'
@@ -13,29 +24,30 @@
1324
var fadeShadow = document.createElement('span')
1425
fadeShadow.className = 'fade-shadow'
1526

16-
button.addEventListener('click', function (e) {
27+
copyButton.addEventListener('click', function (e) {
1728
if (e.target && e.target.matches('a.copy-code-button')) {
1829
navigator.clipboard.writeText(codeBlock.innerText).then(
1930
function () {
2031
/* Chrome doesn't seem to blur automatically,
2132
leaving the button in a focused state. */
22-
button.blur()
33+
copyButton.blur()
2334

24-
button.dataset.title = 'Copied ✓'
35+
copyButton.dataset.title = 'Copied ✓'
2536

2637
setTimeout(function () {
27-
button.dataset.title = 'Copy'
38+
copyButton.dataset.title = 'Copy'
2839
}, 2000)
2940
},
3041
function () {
31-
button.dataset.title = 'Error'
42+
copyButton.dataset.title = 'Error'
3243
}
3344
)
3445
}
3546
})
3647
var pre = codeBlock.parentNode
3748
pre.appendChild(dataSource)
38-
pre.appendChild(button)
49+
if (viewSourceLink) pre.appendChild(viewSourceLink)
50+
pre.appendChild(copyButton)
3951
pre.appendChild(fadeShadow)
4052
})
4153
})()

0 commit comments

Comments
 (0)