|
| 1 | +var path = require('canonical-path'); |
| 2 | +var fs = require("fs"); |
| 3 | +var jsonFile = require('jsonfile'); |
| 4 | + |
| 5 | +/** |
| 6 | + * @dgService linkDocsInlineTagDef |
| 7 | + * @description |
| 8 | + * Process inline link tags (of the form {@linkDocs some/uri 'Some Title'}), replacing them with HTML anchors. |
| 9 | + * The uri should point to a jade page in the Docs without the .jade extension ( under public/docs ). |
| 10 | + * If the title is omitted an attempt will be made to determine the title of the jade page being pointed to. If not found |
| 11 | + * the the title will simply be the last part of the link. |
| 12 | + * Examples |
| 13 | + * {@linkDocs guide/gettingStarted 'QuickStart'} |
| 14 | + * {@linkDocs ts/latest/guide/quickstart } |
| 15 | + * {@linkDocs js/latest/guide/quickstart 'Javascript version of getting started' } |
| 16 | + * {@linkDocs ts/latest/guide/quickstart title="Typescript version of getting started" } |
| 17 | + * @kind function |
| 18 | + * @property {string} lang Default docs API page language when not explicitly given in URI; one of ts|js|dart. |
| 19 | + * @property {string} vers Default docs version. Currently only 'latest'. |
| 20 | + */ |
| 21 | +module.exports = function linkDocsInlineTagDef(parseArgString, createDocMessage, log) { |
| 22 | + var _self = { |
| 23 | + name: 'linkDocs', |
| 24 | + lang: 'ts', |
| 25 | + vers: 'latest', |
| 26 | + description: 'Process inline link tags (of the form {@linkDocs some/uri [title=]"Some Title"}), replacing them with HTML anchors', |
| 27 | + |
| 28 | + handler: function(doc, tagName, tagDescription) { |
| 29 | + // Parse out the uri and title |
| 30 | + var tagArgs = parseArgString(tagDescription); |
| 31 | + var unnamedArgs = tagArgs._; |
| 32 | + var uri = unnamedArgs[0]; |
| 33 | + var title = tagArgs.title || (unnamedArgs.length > 1 ? unnamedArgs[1] : null); |
| 34 | + |
| 35 | + // Are there parameters and/or an anchor? |
| 36 | + var matches, paramAnchor = ''; |
| 37 | + if (matches = uri.match(/([^\#\?]*)([\#\?].*)/)) { |
| 38 | + uri = matches[1]; |
| 39 | + paramAnchor = matches[2]; |
| 40 | + } |
| 41 | + |
| 42 | + // Is this a chapter-relative uri like 'guide/...'? |
| 43 | + if (!uri.match(/^(ts|js|dart)/)) { |
| 44 | + var lang = _self.lang; |
| 45 | + var vers = _self.vers; |
| 46 | + var prevUri = uri; |
| 47 | + uri = path.join(lang, vers, uri); |
| 48 | + log.info('Ajusted linkDocs chapter-relative uri (' + doc.fileInfo.baseName + '): ' + prevUri + ' -> ' + uri); |
| 49 | + } |
| 50 | + |
| 51 | + var isValid = false; |
| 52 | + var jadePath = path.join('./public/docs', uri + '.jade'); |
| 53 | + var key = path.basename(jadePath, '.jade'); |
| 54 | + if ( !fs.existsSync(jadePath)) { |
| 55 | + log.warn(createDocMessage('Invalid docs link (unable to locate jade file: "' + jadePath + '")', doc)); |
| 56 | + } else { |
| 57 | + isValid = true; |
| 58 | + if (!title) { |
| 59 | + var jsonFilePath = path.join(path.dirname(jadePath), '_data.json'); |
| 60 | + if ( fs.existsSync(jsonFilePath)) { |
| 61 | + var jsonObj = jsonFile.readFileSync(jsonFilePath); |
| 62 | + title = jsonObj[key] && jsonObj[key].title; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + var url = path.join('/docs', uri + '.html' + paramAnchor); |
| 67 | + title = title || key || url; |
| 68 | + |
| 69 | + return isValid ? |
| 70 | + '<a href="' + url + '">' + title + '</a>' : |
| 71 | + '<span>' + title + '</span>'; |
| 72 | + } |
| 73 | + }; |
| 74 | + return _self; |
| 75 | +}; |
0 commit comments