Skip to content

Commit a5af45b

Browse files
committed
Set the first page as the home page
1 parent bb73e6a commit a5af45b

12 files changed

+75
-20
lines changed

HISTORY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## master
22

3-
- Table of contents(` docs/README.md`) is now optional
3+
- Table of contents (`docs/README.md`) is now optional
44
- Markdown code is now stripped out of TOC names
5+
- The first page will always be the home page
56

67
## [v0.2.0]
78
> Oct 12, 2015

lib/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ function buildIndex (files, ms, done) {
178178
contents: JSON.stringify(data, null, 2) + '\n'
179179
})
180180

181-
Object.keys(files).forEach((fname) => {
182-
const file = files[fname]
183-
})
184-
185181
done()
186182
}
187183

lib/tocify.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = function tocify (md, files, options) {
2222
var current = re
2323
var docs = options && options.docs || 'docs'
2424
var scope
25+
var i = 0
2526

2627
tokens.forEach((token) => {
2728
switch (token.type) {
@@ -31,7 +32,7 @@ module.exports = function tocify (md, files, options) {
3132
break
3233

3334
case 'text':
34-
current = itemify(token.text, docs, files)
35+
current = itemify(token.text, docs, files, i++)
3536
scope.push(current)
3637
break
3738

@@ -57,7 +58,7 @@ module.exports = function tocify (md, files, options) {
5758
* - `anchor`
5859
*/
5960

60-
function itemify (text, docs, files) {
61+
function itemify (text, docs, files, i) {
6162
const docsExpr = new RegExp('^' + docs + '/')
6263
const current = {}
6364

@@ -83,9 +84,14 @@ function itemify (text, docs, files) {
8384
}
8485
if (source.substr(0, 1) !== '/') source = normalize(docs + '/' + source)
8586
source = source.replace(/^\//, '')
86-
url = source.replace(/\.md$/, '.html')
87-
url = url.replace(/README\.html$/, 'index.html')
88-
url = url.replace(docsExpr, '')
87+
88+
if (i === 0) {
89+
url = 'index.html'
90+
} else {
91+
url = source.replace(/\.md$/, '.html')
92+
url = url.replace(/README\.html$/, 'index.html')
93+
url = url.replace(docsExpr, '')
94+
}
8995
}
9096

9197
current.title = title

test/index/auto_index_test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const compile = require('../../index')()
2+
3+
describe('index/auto index:', function () {
4+
beforeEach(function (done) {
5+
// Mock metalsmith object
6+
var ms = {
7+
metadata () {
8+
return { docs: 'docs' }
9+
}
10+
}
11+
12+
this.files = {
13+
'docs/Readme.md': {
14+
contents: '* [First page](foo/bar/baz.md)'
15+
},
16+
'docs/foo/bar/baz.md': {
17+
contents: '# Introduction\n'
18+
}
19+
}
20+
21+
compile(this.files, ms, (err) => {
22+
if (err) throw err
23+
done()
24+
})
25+
})
26+
27+
it('renders', function () {
28+
expect(this.files['index.html']).toExist()
29+
})
30+
})

test/index/basic_test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ describe('index/basic:', function () {
1111

1212
this.files = {
1313
'docs/README.md': {
14-
contents: '* [Intro](intro.md)'
14+
contents:
15+
'* [My project](../README.md)\n' +
16+
'* [Intro](intro.md)\n'
17+
},
18+
'README.md': {
19+
contents: '# My project\n'
1520
},
1621
'docs/intro.md': {
1722
contents: '# Introduction\n'

test/index/fix_links_with_anchors_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ describe('index/fix links with anchors:', function () {
1212
this.files = {
1313
'docs/README.md': {
1414
contents:
15+
'* [Hi](../README.md)\n' +
1516
'* [Introduction](/docs/introduction.md)\n' +
1617
'* [Getting started](/docs/getting-started.md)\n'
1718
},
19+
'README.md': {
20+
contents: '# hi'
21+
},
1822
'docs/getting-started.md': {
1923
contents: 'hi'
2024
},

test/index/fix_refs_recursive_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ describe('index/fix refs recursive:', function () {
1212
this.files = {
1313
'docs/README.md': {
1414
contents:
15+
'* [My project](/README.md)\n' +
1516
'* [Introduction](/docs/introduction.md)\n' +
1617
'* [Getting started](/docs/getting-started.md)\n'
1718
},
19+
'README.md': {
20+
contents: '# Readme'
21+
},
1822
'docs/getting-started.md': {
1923
contents: 'hi'
2024
},

test/index/linkify_test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ describe('index/linkify:', function () {
1111

1212
this.files = {
1313
'docs/README.md': {
14-
contents: '* [Intro](intro.md)'
14+
contents:
15+
'* [README](../README.md)\n' +
16+
'* [Intro](intro.md)'
17+
},
18+
'README.md': {
19+
contents: '# Readme'
1520
},
1621
'docs/intro.md': {
1722
contents: 'http://google.com'

test/index/readme_case_sensitivity_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ describe('index/readme case sensitivity:', function () {
1111

1212
this.files = {
1313
'docs/Readme.md': {
14-
contents: '* [Intro](intro.md)'
14+
contents: '* [Intro](../README.md)'
1515
},
16-
'docs/intro.md': {
16+
'README.md': {
1717
contents: '# Introduction\n'
1818
}
1919
}
@@ -25,6 +25,6 @@ describe('index/readme case sensitivity:', function () {
2525
})
2626

2727
it('renders', function () {
28-
expect(this.files['intro.html']).toExist()
28+
expect(this.files['index.html']).toExist()
2929
})
3030
})

test/index/toc_with_anchor_test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ describe('index/toc with anchor:', function () {
1111

1212
this.files = {
1313
'docs/README.md': {
14-
contents: '* [Intro](intro.md#xyz)'
14+
contents:
15+
'* [Readme](../README.md)\n' +
16+
'* [Intro](intro.md#xyz)\n'
17+
},
18+
'README.md': {
19+
contents: '# Readme'
1520
},
1621
'docs/intro.md': {
1722
contents: '# Introduction\n'
@@ -29,6 +34,6 @@ describe('index/toc with anchor:', function () {
2934
})
3035

3136
it('sets .anchor', function () {
32-
expect(this.files['_docpress.json'].toc.sections[0].anchor).toEqual('#xyz')
37+
expect(this.files['_docpress.json'].toc.sections[1].anchor).toEqual('#xyz')
3338
})
3439
})

0 commit comments

Comments
 (0)