git ssb

2+

cel / scuttlebot.io



Commit 374fbbbc3071c93a1e6d1403f2621df1f193bc05

add table-of-contents component

Paul Frazee committed on 3/16/2016, 7:05:29 PM
Parent: bde700450bd40b72df90242bf3588a793f98aa13

Files changed

markdown.jschanged
tmpl/com.part.jschanged
tmpl/css/com.part.jschanged
tmpl/docs/advanced/advanced-queries.mdchanged
tmpl/head.part.jschanged
tmpl/js/table-of-contents.jsadded
markdown.jsView
@@ -15,12 +15,34 @@
1515 attributes: { class: 'anchor' },
1616 template: linkSvg
1717 })
1818 .use(html)
19+ .use(injectTOC)
1920 .use(transformCodeExamples)
2021 .process(text)
2122 }
2223
24+// find all h2s and create a dropdown table-of-contents
25+function injectTOC (remark, options) {
26+ remark.Compiler.prototype.visitors.tableOfContents = renderTOC
27+ return ast => {
28+ var headings = ast.children.filter(node => node.type == 'heading' && node.depth == 2)
29+ if (headings.length > 2)
30+ createTOC(ast, headings)
31+ return ast
32+ }
33+}
34+
35+// add the tableOfContents node to the ast
36+function createTOC (ast, headings) {
37+ ast.children.unshift({ type: 'tableOfContents', children: headings })
38+}
39+
40+// render the tableOfContents widget
41+function renderTOC (node, root) {
42+ return com.tableOfContents(node.children)
43+}
44+
2345 // find any <code> sections and group them together into our code-examples component
2446 function transformCodeExamples (remark, options) {
2547 remark.Compiler.prototype.visitors.codeExamples = renderCodeExamples
2648 return ast => {
@@ -60,16 +82,16 @@
6082 var len = end - start
6183 ast.children.splice(start-offset, len, {
6284 type: 'codeExamples',
6385 children: ast.children.slice(start-offset, start-offset+len),
64- position: false // TODO - what is this?
86+ position: false // TODO - needed? doesnt look like it
6587 })
6688 offset += len - 1
6789 })
6890 }
6991
7092 // convert from AST to html
71-function renderCodeExamples (node, ast) {
93+function renderCodeExamples (node, root) {
7294 var codes = {}
7395 node.children.forEach(node => {
7496 if (node.type == 'code' && !!node.lang)
7597 codes[node.lang] = node.value
tmpl/com.part.jsView
@@ -10,8 +10,20 @@
1010 </div>
1111 </div>`
1212 }
1313
14+module.exports.tableOfContents = items => {
15+ items = items
16+ .map(item => [item.children[0].url, item.children[1].value])
17+ .filter(item => item[0] && item[1])
18+ return `<div class="table-of-contents">
19+ <a href="#">Jump to...</a>
20+ <ul>
21+ ${ items.map(item => `<li><a href="${item[0]}">${item[1]}</a></li>`).join('') }
22+ </ul>
23+ </div>`
24+}
25+
1426 // enforce an order to the language tabs
1527 function langPrioritySort (a, b) {
1628 if (b == 'js' && a != 'js')
1729 return 1
tmpl/css/com.part.jsView
@@ -77,5 +77,27 @@
7777 }
7878 .code-examples pre.current {
7979 display: block;
8080 }
81+
82+.table-of-contents ul {
83+ display: none;
84+ list-style: none;
85+}
86+.table-of-contents.expanded ul {
87+ display: block;
88+ padding: 0;
89+ margin: 0;
90+}
91+.table-of-contents ul li:nth-child(even) {
92+ background: #fafafa;
93+}
94+.table-of-contents ul li:hover {
95+ background: #eee;
96+}
97+.table-of-contents ul li a {
98+ display: block;
99+ padding: 0.5em;
100+ color: #555;
101+ text-decoration: none;
102+}
81103 `
tmpl/docs/advanced/advanced-queries.mdView
@@ -1,5 +1,5 @@
1-## Get messages by timestamp, from newest to oldest
1+## Messages ordered by timestamp
22
33 ```js
44 var pull = require('pull-stream')
55 pull(
@@ -17,9 +17,9 @@
1717 [&rarr; createFeedStream API](/apis/scuttlebot/ssb.html#createfeedstream-source)
1818
1919 ---
2020
21-## Get messages by receive-time, from newest to oldest
21+## Messages ordered by receive-time
2222
2323 ```js
2424 var pull = require('pull-stream')
2525 pull(
@@ -37,9 +37,9 @@
3737 [&rarr; createLogStream API](/apis/scuttlebot/ssb.html#createlogstream-source)
3838
3939 ---
4040
41-## Get post-messages by receive-time
41+## Messages by type
4242
4343 ```js
4444 var pull = require('pull-stream')
4545 pull(
@@ -56,9 +56,9 @@
5656 [&rarr; messagesByType API](/apis/scuttlebot/ssb.html#messagesbytype-source)
5757
5858 ---
5959
60-## Get all messages from a user
60+## Messages by user
6161
6262 ```js
6363 var pull = require('pull-stream')
6464 pull(
@@ -75,45 +75,45 @@
7575 [&rarr; createUserStream API](/apis/scuttlebot/ssb.html#createuserstream-source)
7676
7777 ---
7878
79-## Watch for new messages from a user
79+## Watch for new messages
8080
8181 ```js
8282 var pull = require('pull-stream')
8383 pull(
84- sbot.createUserStream({ id: userId, live: true }),
84+ sbot.createLogStream({ live: true }),
8585 pull.drain(function (msg) { ... })
8686 )
8787 ```
8888 ```bash
89-sbot createUserStream --id {userId} --live
89+sbot log --live
9090 ```
9191
92-Notice that `pull.drain` is used instead of `pull.collect`, so that new messages are handled immediately.
92+[&rarr; createLogStream API](/apis/scuttlebot/ssb.html#createlogstream-source)
9393
94-[&rarr; createUserStream API](/apis/scuttlebot/ssb.html#createuserstream-source)
95-
9694 ---
9795
98-## Watch for new messages from anybody
96+## Watch for new messages by a user
9997
10098 ```js
10199 var pull = require('pull-stream')
102100 pull(
103- sbot.createLogStream({ live: true }),
101+ sbot.createUserStream({ id: userId, live: true }),
104102 pull.drain(function (msg) { ... })
105103 )
106104 ```
107105 ```bash
108-sbot log --live
106+sbot createUserStream --id {userId} --live
109107 ```
110108
111-[&rarr; createLogStream API](/apis/scuttlebot/ssb.html#createlogstream-source)
109+Notice that `pull.drain` is used instead of `pull.collect`, so that new messages are handled immediately.
112110
111+[&rarr; createUserStream API](/apis/scuttlebot/ssb.html#createuserstream-source)
112+
113113 ---
114114
115-## Get all about messages for a user
115+## About-user messages
116116
117117 ```js
118118 var pull = require('pull-stream')
119119 pull(
@@ -131,9 +131,9 @@
131131 [&rarr; links API](/apis/scuttlebot/ssb.html#links-source)
132132
133133 ---
134134
135-## Get all votes on a message
135+## Votes on a message
136136
137137 ```js
138138 var pull = require('pull-stream')
139139 pull(
@@ -151,9 +151,9 @@
151151 [&rarr; links API](/apis/scuttlebot/ssb.html#links-source)
152152
153153 ---
154154
155-## Get all files referenced by a user
155+## Files referenced by a user
156156
157157 ```js
158158 var pull = require('pull-stream')
159159 pull(
@@ -164,13 +164,18 @@
164164 ```bash
165165 sbot links --source {userId} --dest &
166166 ```
167167
168+All IDs have a "sigil" character defining what type of object the ID references.
169+(Blobs start with "&", users start with "@", and messages start with "%").
170+
171+In `links`, you can use the sigil to filter the source or dest by the ID type.
172+
168173 [&rarr; links API](/apis/scuttlebot/ssb.html#links-source)
169174
170175 ---
171176
172-## Get all links from one user to another
177+## Links from one user to another
173178
174179 ```js
175180 var pull = require('pull-stream')
176181 pull(
@@ -185,9 +190,9 @@
185190 [&rarr; links API](/apis/scuttlebot/ssb.html#links-source)
186191
187192 ---
188193
189-## Get a full post-thread
194+## Full post-thread
190195
191196 ```js
192197 sbot.relatedMessages({ id: messageId }, function (err, thread) {
193198 ...
tmpl/head.part.jsView
@@ -10,6 +10,7 @@
1010 <link rel="stylesheet" href="/css/index.css">
1111 <link rel="stylesheet" href="/css/prism.css">
1212 <script src="/js/prism.js"></script>
1313 <script src="/js/code-examples.js"></script>
14+ <script src="/js/table-of-contents.js"></script>
1415 </head>`
1516 }
tmpl/js/table-of-contents.jsView
@@ -1,0 +1,24 @@
1+window.addEventListener('load', function () {
2+ setupTOC(document.querySelector('.table-of-contents'))
3+})
4+
5+function setupTOC (toc) {
6+ // attach click handlers
7+ toc.querySelector('a').addEventListener('click', function (e) {
8+ e.preventDefault()
9+ e.stopPropagation()
10+ toc.classList.add('expanded')
11+ })
12+ document.body.addEventListener('click', function () {
13+ toc.classList.remove('expanded')
14+ })
15+}
16+
17+function selectTab (el, n) {
18+ // deselect current
19+ try { el.querySelector('pre.current').classList.remove('current') } catch (e) {}
20+ try { el.querySelector('.tab.current').classList.remove('current') } catch (e) {}
21+ // select new
22+ el.querySelector('pre:nth-child('+n+')').classList.add('current')
23+ el.querySelector('.tab:nth-child('+n+')').classList.add('current')
24+}

Built with git-ssb-web