Skip to content

Commit bf03eb0

Browse files
committed
Added SiteMap generation to the documentation
1 parent c2f2587 commit bf03eb0

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

docs/spec/sitemapSpec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var SiteMap = require('sitemap.js').SiteMap;
2+
var Doc = require('ngdoc.js').Doc;
3+
4+
5+
describe('sitemap', function(){
6+
it('should render empty sitemap', function(){
7+
var map = new SiteMap([]);
8+
expect(map.render()).toEqual([
9+
'<?xml version="1.0" encoding="UTF-8"?>',
10+
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
11+
'</sitemapindex>', ''].join('\n'));
12+
});
13+
14+
it('should render ngdoc url', function(){
15+
var map = new SiteMap([new Doc({name: 'a.b.c<>\'"&'})]);
16+
expect(map.render()).toContain([
17+
' <url>',
18+
'<loc>http://docs.angularjs.org/#!a.b.c&lt;&gt;&apos;&quot;&amp;</loc>',
19+
'<changefreq>weekly</changefreq>',
20+
'</url>'].join(''));
21+
22+
});
23+
});

docs/src/SiteMap.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
exports.SiteMap = SiteMap;
2+
3+
/**
4+
* @see http://www.sitemaps.org/protocol.php
5+
*
6+
* @param docs
7+
* @returns {SiteMap}
8+
*/
9+
function SiteMap(docs){
10+
this.render = function(){
11+
var map = [];
12+
map.push('<?xml version="1.0" encoding="UTF-8"?>');
13+
map.push('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
14+
docs.forEach(function(doc){
15+
map.push(' <url><loc>http://docs.angularjs.org/#!' +
16+
encode(doc.name) + '</loc><changefreq>weekly</changefreq></url>');
17+
});
18+
map.push('</sitemapindex>');
19+
map.push('');
20+
return map.join('\n');
21+
};
22+
23+
function encode(text){
24+
return text
25+
.replace(/&/mg, '&amp;')
26+
.replace(/</mg, '&lt;')
27+
.replace(/>/mg, '&gt;')
28+
.replace(/'/mg, '&apos;')
29+
.replace(/"/mg, '&quot;');
30+
}
31+
}

docs/src/gen-docs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ require.paths.push('lib');
33
var reader = require('reader.js'),
44
ngdoc = require('ngdoc.js'),
55
writer = require('writer.js'),
6-
callback = require('callback.js');
6+
callback = require('callback.js'),
7+
SiteMap = require('SiteMap.js').SiteMap;
78

89
var docs = [];
910
var start;
@@ -30,6 +31,8 @@ var writes = callback.chain(function(){
3031
writer.copy('doc_widgets.css', writes.waitFor());
3132
writer.copy('docs-scenario.html', writes.waitFor());
3233
writer.output('docs-scenario.js', ngdoc.scenarios(docs), writes.waitFor());
34+
writer.output('sitemap.xml', new SiteMap(docs).render(), writes.waitFor());
35+
writer.output('robots.txt', 'Sitemap: http://docs.angularjs.org/sitemap.xml\n', writes.waitFor());
3336
writer.copy('syntaxhighlighter/shBrushJScript.js', writes.waitFor());
3437
writer.copy('syntaxhighlighter/shBrushXml.js', writes.waitFor());
3538
writer.copy('syntaxhighlighter/shCore.css', writes.waitFor());

0 commit comments

Comments
 (0)