diff --git a/aio/transforms/angular.io-package/index.js b/aio/transforms/angular.io-package/index.js index c6e3f928c4..da97986215 100644 --- a/aio/transforms/angular.io-package/index.js +++ b/aio/transforms/angular.io-package/index.js @@ -49,6 +49,7 @@ module.exports = .processor(require('./processors/markBarredODocsAsPrivate')) .processor(require('./processors/filterPrivateDocs')) .processor(require('./processors/filterIgnoredDocs')) + .processor(require('./processors/fixInternalDocumentLinks')) // overrides base packageInfo and returns the one for the 'angular/angular' repo. .factory('packageInfo', function() { return require(path.resolve(PROJECT_ROOT, 'package.json')); }) diff --git a/aio/transforms/angular.io-package/processors/fixInternalDocumentLinks.js b/aio/transforms/angular.io-package/processors/fixInternalDocumentLinks.js new file mode 100644 index 0000000000..3e95c4cf1f --- /dev/null +++ b/aio/transforms/angular.io-package/processors/fixInternalDocumentLinks.js @@ -0,0 +1,24 @@ +/** + * @dgProcessor fixInternalDocumentLinks + * @description + * Add in the document path to links that start with a hash. + * This is important when the web app has a base href in place, + * since links like: `` would get mapped to + * the URL `base/#some-id` even if the current location is `base/some/doc`. + */ +module.exports = function fixInternalDocumentLinks() { + + var INTERNAL_LINK = /(]*href=")(#[^"]*)/gm; + + return { + $runAfter: ['inlineTagProcessor'], + $runBefore: ['writeFilesProcessor'], + $process: function(docs) { + docs.forEach(doc => { + doc.renderedContent = doc.renderedContent.replace(INTERNAL_LINK, (_, pre, hash) => { + return pre + doc.path + hash; + }); + }); + } + }; +}; \ No newline at end of file diff --git a/aio/transforms/angular.io-package/processors/fixInternalDocumentLinks.spec.js b/aio/transforms/angular.io-package/processors/fixInternalDocumentLinks.spec.js new file mode 100644 index 0000000000..07e8e8d595 --- /dev/null +++ b/aio/transforms/angular.io-package/processors/fixInternalDocumentLinks.spec.js @@ -0,0 +1,52 @@ +const testPackage = require('../../helpers/test-package'); +const processorFactory = require('./fixInternalDocumentLinks'); +const Dgeni = require('dgeni'); + +describe('fixInternalDocumentLinks processor', () => { + + it('should be available on the injector', () => { + const dgeni = new Dgeni([testPackage('angular.io-package')]); + const injector = dgeni.configureInjector(); + const processor = injector.get('fixInternalDocumentLinks'); + expect(processor.$process).toBeDefined(); + }); + + it('should run before the correct processor', () => { + const processor = processorFactory(); + expect(processor.$runBefore).toEqual(['writeFilesProcessor']) + }); + + it('should run before the correct processor', () => { + const processor = processorFactory(); + expect(processor.$runAfter).toEqual(['inlineTagProcessor']); + }); + + it('should prefix internal hash links with the current doc path', () => { + const processor = processorFactory(); + const docs = [ + { + path: 'some/doc', + renderedContent: ` + Google + Some Id + Link to heading + Link to heading + Link to heading + ` + }, + ]; + processor.$process(docs); + expect(docs).toEqual([ + { + path: 'some/doc', + renderedContent: ` + Google + Some Id + Link to heading + Link to heading + Link to heading + ` + }, + ]); + }) +}); \ No newline at end of file