mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-13 17:23:11 +00:00
Allow relative links in docs
So you can use links without section when they link within the section.
This commit is contained in:
parent
e389911a35
commit
43b2cd45f0
2 changed files with 42 additions and 36 deletions
|
|
@ -104,6 +104,23 @@ describe('ngdoc', function(){
|
||||||
expect(doc.links).toContain('api/angular.link');
|
expect(doc.links).toContain('api/angular.link');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('convertUrlToAbsolute', function() {
|
||||||
|
var doc;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
doc = new Doc({section: 'section'});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not change absolute url', function() {
|
||||||
|
expect(doc.convertUrlToAbsolute('guide/index')).toEqual('guide/index');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should prepend current section to relative url', function() {
|
||||||
|
expect(doc.convertUrlToAbsolute('angular.widget')).toEqual('section/angular.widget');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('sorting', function(){
|
describe('sorting', function(){
|
||||||
function property(name) {
|
function property(name) {
|
||||||
return function(obj) {return obj[name];};
|
return function(obj) {return obj[name];};
|
||||||
|
|
@ -366,7 +383,10 @@ describe('ngdoc', function(){
|
||||||
'external{@link http://angularjs.org}\n\n' +
|
'external{@link http://angularjs.org}\n\n' +
|
||||||
'external{@link ./static.html}\n\n' +
|
'external{@link ./static.html}\n\n' +
|
||||||
'{@link angular.directive.ng:foo ng:foo}');
|
'{@link angular.directive.ng:foo ng:foo}');
|
||||||
|
|
||||||
|
doc.section = 'api';
|
||||||
doc.parse();
|
doc.parse();
|
||||||
|
|
||||||
expect(doc.description).
|
expect(doc.description).
|
||||||
toContain('foo <a href="#!api/angular.foo"><code>angular.foo</code></a>');
|
toContain('foo <a href="#!api/angular.foo"><code>angular.foo</code></a>');
|
||||||
expect(doc.description).
|
expect(doc.description).
|
||||||
|
|
|
||||||
|
|
@ -57,36 +57,25 @@ Doc.prototype = {
|
||||||
return words.join(' ');
|
return words.join(' ');
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
/*
|
* Converts relative urls (without section) into absolute
|
||||||
* This function is here to act as a huristic based translator from the old style urls to
|
* Absolute url means url with section
|
||||||
* the new style which use sections.
|
*
|
||||||
|
* @example
|
||||||
|
* - if the link is inside any api doc:
|
||||||
|
* angular.widget -> api/angular.widget
|
||||||
|
*
|
||||||
|
* - if the link is inside any guid doc:
|
||||||
|
* intro -> guide/intro
|
||||||
|
*
|
||||||
|
* @param {string} url Absolute or relative url
|
||||||
|
* @returns {string} Absolute url
|
||||||
*/
|
*/
|
||||||
sectionHuristic: function (url){
|
convertUrlToAbsolute: function(url) {
|
||||||
// if we are new styl URL with section/id then just return;
|
|
||||||
if (url.match(/\//)) return url;
|
if (url.match(/\//)) return url;
|
||||||
var match = url.match(/(\w+)(\.(.*))?/);
|
|
||||||
var section = match[1];
|
// remove this after
|
||||||
var id = match[3] || 'index';
|
return this.section + '/' + url;
|
||||||
switch(section) {
|
|
||||||
case 'angular':
|
|
||||||
section = 'api';
|
|
||||||
id = 'angular.' + id;
|
|
||||||
break;
|
|
||||||
case 'api':
|
|
||||||
case 'cookbook':
|
|
||||||
case 'guide':
|
|
||||||
case 'intro':
|
|
||||||
case 'tutorial':
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
id = section + '.' + id;
|
|
||||||
section = 'intro';
|
|
||||||
}
|
|
||||||
var newUrl = section + '/' + (id || 'index');
|
|
||||||
console.log('WARNING:', 'found old style url', url, 'at', this.file, this.line,
|
|
||||||
'converting to', newUrl);
|
|
||||||
return newUrl;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
markdown: function (text) {
|
markdown: function (text) {
|
||||||
|
|
@ -126,16 +115,13 @@ Doc.prototype = {
|
||||||
text = text.replace(/{@link\s+([^\s}]+)\s*([^}]*?)\s*}/g,
|
text = text.replace(/{@link\s+([^\s}]+)\s*([^}]*?)\s*}/g,
|
||||||
function(_all, url, title){
|
function(_all, url, title){
|
||||||
var isFullUrl = url.match(IS_URL),
|
var isFullUrl = url.match(IS_URL),
|
||||||
// FIXME(vojta) angular link could be api.angular now with sections
|
// FIXME(vojta) angular link could be api/angular now with sections
|
||||||
isAngular = url.match(IS_ANGULAR);
|
isAngular = url.match(IS_ANGULAR),
|
||||||
|
absUrl = isFullUrl ? url : self.convertUrlToAbsolute(url);
|
||||||
|
|
||||||
if (!isFullUrl) {
|
if (!isFullUrl) self.links.push(absUrl);
|
||||||
// TODO(vojta) there could be relative link, but not angular
|
|
||||||
// do we want to store all links (and check even the full links like http://github.com ?
|
|
||||||
self.links.push(self.sectionHuristic(url));
|
|
||||||
}
|
|
||||||
|
|
||||||
return '<a href="' + (isFullUrl ? '' + url : '#!' + self.sectionHuristic(url)) + '">'
|
return '<a href="' + (isFullUrl ? '' + url : '#!' + absUrl) + '">'
|
||||||
+ (isAngular ? '<code>' : '')
|
+ (isAngular ? '<code>' : '')
|
||||||
+ (title || url).replace(/\n/g, ' ')
|
+ (title || url).replace(/\n/g, ' ')
|
||||||
+ (isAngular ? '</code>' : '')
|
+ (isAngular ? '</code>' : '')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue