-
Notifications
You must be signed in to change notification settings - Fork 46
Full Markdown Support #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0b13ad5
First steps to index.md and readme.md support
robhrt7 163a1b5
Merge remote-tracking branch 'origin/master' into operatino/markdown-…
robhrt7 25a6eba
Extended index.md support
robhrt7 ec8edb5
Transfering docs to Markdown files
robhrt7 79991d8
Markdown support doc
robhrt7 94f2b7a
Merge remote-tracking branch 'origin/0.4.1' into operatino/markdown-s…
robhrt7 7e7b548
Markdown support: review fixes, removing redirection to clean spec path
robhrt7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| 'use strict'; | ||
|
|
||
| var cheerio = require('cheerio'); | ||
| var prettyHrtime = require('pretty-hrtime'); | ||
|
|
||
| var marked = require('marked'); | ||
| var renderer = new marked.Renderer(); | ||
| marked.setOptions({ | ||
| renderer: renderer | ||
| }); | ||
|
|
||
| /* | ||
| * Get file content from response and parse contained Markdown markup | ||
| * | ||
| * @param {object} req - Request object | ||
| * @param {object} res - Response object | ||
| * @param {function} next - The callback function | ||
| * */ | ||
| exports.process = function (req, res, next) { | ||
| if (req.specData && req.specData.renderedHtml && req.specData.isMd) { | ||
| var start = process.hrtime(); | ||
| var input = req.specData.renderedHtml; | ||
|
|
||
| // Processing with native markdown renderer | ||
| renderer.code = function (code, language) { | ||
| if (language === 'example') { | ||
| return '<div class="source_example">'+ code +'</div>'; | ||
| } else if (language && language !== '') { | ||
| return '<code class="src-'+ language +' source_visible">'+ code +'</code>'; | ||
| } else { | ||
| return '<pre><code class="lang-source_wide-code">'+ code +'</code></pre>'; | ||
| } | ||
| }; | ||
|
|
||
| var $ = cheerio.load(marked(input)); | ||
|
|
||
| // Spec description | ||
| var $H1 = $('h1'); | ||
| var $afterH1 = $H1.nextUntil('h2'); | ||
| $afterH1.remove(); | ||
| $H1.after('<div class="source_info">'+ $afterH1 +'</div>'); | ||
|
|
||
| // Spec sections | ||
| $('h2').each(function(){ | ||
| var $this = $(this); | ||
| var $sectionElems = $this.nextUntil('h2'); | ||
| var id = $this.attr('id'); | ||
| $this.removeAttr('id'); | ||
| $sectionElems.remove(); | ||
|
|
||
| $(this).replaceWith([ | ||
| '<div class="source_section" id="'+ id +'">', | ||
| $this + $sectionElems, | ||
| '</div>' | ||
| ].join('')); | ||
|
|
||
| }); | ||
|
|
||
| req.specData.renderedHtml = $.html(); | ||
|
|
||
| var end = process.hrtime(start); | ||
| global.log.debug('Markdown processing took: ', prettyHrtime(end)); | ||
|
|
||
| next(); | ||
| } else { | ||
| next(); | ||
| } | ||
| }; | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,134 +5,123 @@ var path = require('path'); | |
| var pathToApp = path.dirname(require.main.filename); | ||
|
|
||
| /** | ||
| * Get spec content and write it to request | ||
| * Handling Spec request | ||
| * | ||
| * @param {object} req - Request object | ||
| * @param {object} res - Response object | ||
| * @param {function} next - The callback function | ||
| * */ | ||
| var handleRequest = function(req, res, next) { | ||
| var handleSpec = function(req, res, next) { | ||
| // Filled during middleware processing | ||
| req.specData = {}; | ||
|
|
||
| // get the physical path of a requested file | ||
| var physicalPath = global.app.get('user') + req.path; | ||
| // Get the physical path of a requested file | ||
| var physicalPath = path.join(global.app.get('user'), req.path); | ||
|
|
||
| // TODO: move to config with array of exclusions | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failed promise (TODO)? |
||
| if (req.path.lastIndexOf('/docs/', 0) === 0) { | ||
| physicalPath = pathToApp + req.path; | ||
| } | ||
|
|
||
| // Extension of a requested file | ||
| var extension = path.extname(physicalPath).replace(".", ""); | ||
| var directory = path.dirname(physicalPath); // get the dir of a requested file | ||
| //var filename = path.basename(physicalPath); // filename of a requested file | ||
| var extension = path.extname(physicalPath).replace(".", ""); // extension of a requested file | ||
| var supportedExtensions = global.opts.core.common.extensions; | ||
| var extIndex = supportedExtensions.indexOf(extension); | ||
|
|
||
| var infoJson = directory + '/' + global.opts.core.common.infoFile; | ||
|
|
||
| // in case if one of supported filetypes is requested | ||
| if (extIndex >= 0) { | ||
| fs.exists(physicalPath, function(exists) { | ||
|
|
||
| if (exists) { | ||
| fs.readFile(physicalPath, 'utf8', function (err, data) { | ||
| data = data.replace(/^\s+|\s+$/g, ''); | ||
| if (err) { | ||
| res.send(err); | ||
| } else { | ||
|
|
||
| fs.readFile(infoJson, 'utf8', function (err, info) { | ||
| if (err) { | ||
| info = { | ||
| title: "New spec", | ||
| author: "Anonymous", | ||
| keywords: "" | ||
| }; | ||
| } else { | ||
| info = JSON.parse(info); | ||
| } | ||
|
|
||
| // if requested file is one of supported filetypes, then write proper flag to request. f.e. req.specData.isJade; // true | ||
| if (extension === supportedExtensions[extIndex]) { | ||
| var capitalizedExtension = extension.charAt(0).toUpperCase() + extension.slice(1); | ||
| req.specData["is" + capitalizedExtension] = true; | ||
| } | ||
|
|
||
| req.specData.info = info; // add spec info object to request | ||
| req.specData.renderedHtml = data; // add spec content to request | ||
|
|
||
| next(); | ||
| }); | ||
| } | ||
|
|
||
| }); | ||
| fs.readFile(physicalPath, 'utf8', function (err, data) { | ||
| if (err) { | ||
| res.send(err); | ||
| return; | ||
| } | ||
|
|
||
| data = data.replace(/^\s+|\s+$/g, ''); | ||
|
|
||
| fs.readFile(infoJson, 'utf8', function (err, info) { | ||
| if (err) { | ||
| info = { | ||
| title: "New spec", | ||
| author: "Anonymous", | ||
| keywords: "" | ||
| }; | ||
| } else { | ||
| next(); | ||
| info = JSON.parse(info); | ||
| } | ||
|
|
||
| var capitalizedExtension = extension.charAt(0).toUpperCase() + extension.slice(1); | ||
|
|
||
| req.specData["is" + capitalizedExtension] = true; | ||
| req.specData.info = info; // add spec info object to request | ||
| req.specData.renderedHtml = data; // add spec content to request | ||
|
|
||
| next(); | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Checking if Spec is requested | ||
| * | ||
| * @param {object} req - Request object | ||
| * @param {object} res - Response object | ||
| * @param {function} next - The callback function | ||
| * */ | ||
| exports.process = function(req, res, next) { | ||
| // Get the physical path of a requested file | ||
| var physicalPath = global.app.get('user') + req.path; | ||
|
|
||
| // TODO: move to config with array of exclusions | ||
| if (req.path.lastIndexOf('/docs/', 0) === 0) { | ||
| physicalPath = pathToApp + req.path; | ||
| } | ||
| // if directory is requested | ||
| else if (extension === "") { | ||
|
|
||
| // Extension of a requested file | ||
| var extension = path.extname(physicalPath).replace(".", ""); | ||
|
|
||
| // Check if folder is requested | ||
| if (extension === "") { | ||
| var requestedDir = req.path; | ||
| var supportedExtensions = global.opts.core.common.extensions; | ||
|
|
||
| // append trailing slash | ||
| // Append trailing slash | ||
| if (requestedDir.slice(-1) !== '/') { | ||
| requestedDir += '/'; | ||
| } | ||
|
|
||
| var oneOfExtensionsFound = false; | ||
|
|
||
| for (var j = 0; j < supportedExtensions.length; j++) { | ||
| var fileName = "index." + supportedExtensions[j]; | ||
|
|
||
| if (fs.existsSync(physicalPath + fileName)) { | ||
| var specNotFileFound = true; | ||
| var checkingSpecFile = function(supportedIndexFormat){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be it would be better to move this function out of export.process? |
||
| if (specNotFileFound && fs.existsSync(physicalPath + supportedIndexFormat)) { | ||
| // Passing req params | ||
| var urlParams = req.url.split('?')[1]; | ||
| var paramsString = urlParams ? '?' + urlParams : ''; | ||
| req.url = requestedDir + fileName + paramsString; | ||
|
|
||
| // recursive call | ||
| handleRequest(req, res, next); | ||
| // Modifying url and saving params string | ||
| req.url = requestedDir + supportedIndexFormat + paramsString; | ||
|
|
||
| oneOfExtensionsFound = true; | ||
| break; | ||
| // Recursive call | ||
| handleSpec(req, res, next); | ||
|
|
||
| specNotFileFound = false; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| if (!oneOfExtensionsFound) { | ||
| next(); | ||
| // First check if any supported file exists in dir | ||
| for (var j = 0; j < supportedExtensions.length; j++) { | ||
| if (specNotFileFound) { | ||
| var supportedIndexFormat = "index." + supportedExtensions[j]; | ||
|
|
||
| checkingSpecFile(supportedIndexFormat); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| } else { | ||
| next(); | ||
| } | ||
| }; | ||
| // Then check if component have readme.md | ||
| checkingSpecFile('readme.md'); | ||
|
|
||
| /** | ||
| * check if requested file is *.src and render | ||
| * | ||
| * @param {object} req - Request object | ||
| * @param {object} res - Response object | ||
| * @param {function} next - The callback function | ||
| * */ | ||
| exports.process = function (req, res, next) { | ||
| handleRequest(req, res, next); | ||
| }; | ||
| if (specNotFileFound) next(); | ||
|
|
||
| /** | ||
| * if URL ends with "index.src" => redirect to trailing slash | ||
| * | ||
| * @param {object} req - Request object | ||
| * @param {object} res - Response object | ||
| * @param {function} next - The callback function | ||
| * */ | ||
| exports.handleIndex = function (req, res, next) { | ||
| if (req.path.slice(-9) === 'index.src') { | ||
| // Keeping params on redirect | ||
| var urlParams = req.url.split('?')[1]; | ||
| var paramsSting = urlParams ? '?' + urlParams : ''; | ||
| res.redirect(301, req.path.slice(0, -9) + paramsSting); | ||
| } else { | ||
| next(); | ||
| } | ||
| }; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it would be great to have an ability to configure/redefine this structure (I mean h1,h2 usage), isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's predefined markup, that will be also generated from
info.json, I think configuring this one will be an overhead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does't seem to be the real overhead, strictly speaking. Anyway, it's up to you. I'm not quite sure in it.