summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-06-29 10:31:06 +0200
committerSamy Pessé <samypesse@gmail.com>2016-06-29 10:31:06 +0200
commitc9c2dde2b63505177265c66b1a6c4dd358415416 (patch)
tree444b6a539c93f677c6f8ee5eac67dca8de7cdab8
parentd9a1d387c7a61aa18cdb9b2916bc761e0f902804 (diff)
downloadgitbook-c9c2dde2b63505177265c66b1a6c4dd358415416.zip
gitbook-c9c2dde2b63505177265c66b1a6c4dd358415416.tar.gz
gitbook-c9c2dde2b63505177265c66b1a6c4dd358415416.tar.bz2
Add method "isFile" and "isReadme" in SummaryArticle
-rw-r--r--lib/models/__tests__/summaryArticle.js30
-rw-r--r--lib/models/summaryArticle.js33
-rw-r--r--lib/output/getModifiers.js10
-rw-r--r--lib/output/helper/fileToOutput.js12
-rw-r--r--lib/output/helper/resolveFileToURL.js12
-rw-r--r--lib/output/modifiers/annotateText.js19
-rw-r--r--lib/utils/location.js86
7 files changed, 137 insertions, 65 deletions
diff --git a/lib/models/__tests__/summaryArticle.js b/lib/models/__tests__/summaryArticle.js
index 7c4bc57..22a7a20 100644
--- a/lib/models/__tests__/summaryArticle.js
+++ b/lib/models/__tests__/summaryArticle.js
@@ -1,4 +1,5 @@
var SummaryArticle = require('../summaryArticle');
+var File = require('../file');
describe('SummaryArticle', function() {
describe('createChildLevel', function() {
@@ -18,6 +19,35 @@ describe('SummaryArticle', function() {
expect(article.createChildLevel()).toBe('1.1.2');
});
});
+
+ describe('isFile', function() {
+ it('must return true when exactly the file', function() {
+ var article = SummaryArticle.create({
+ ref: 'hello.md'
+ }, '1.1');
+ var file = File.createWithFilepath('hello.md');
+
+ expect(article.isFile(file)).toBe(true);
+ });
+
+ it('must return true when path is not normalized', function() {
+ var article = SummaryArticle.create({
+ ref: '/hello.md'
+ }, '1.1');
+ var file = File.createWithFilepath('hello.md');
+
+ expect(article.isFile(file)).toBe(true);
+ });
+
+ it('must return false when has anchor', function() {
+ var article = SummaryArticle.create({
+ ref: 'hello.md#world'
+ }, '1.1');
+ var file = File.createWithFilepath('hello.md');
+
+ expect(article.isFile(file)).toBe(false);
+ });
+ });
});
diff --git a/lib/models/summaryArticle.js b/lib/models/summaryArticle.js
index 9b5b653..6da8d1d 100644
--- a/lib/models/summaryArticle.js
+++ b/lib/models/summaryArticle.js
@@ -40,7 +40,8 @@ SummaryArticle.prototype.getDepth = function() {
};
/**
- * Get path (without anchor) to the pointing file
+ * Get path (without anchor) to the pointing file.
+ * It also normalizes the file path.
*
* @return {String}
*/
@@ -58,8 +59,8 @@ SummaryArticle.prototype.getPath = function() {
var pathname = (parts.length > 1? parts.slice(0, -1).join('#') : ref);
- // Normalize path to remove ('./', etc)
- return location.normalize(pathname);
+ // Normalize path to remove ('./', '/...', etc)
+ return location.flatten(pathname);
};
/**
@@ -107,6 +108,32 @@ SummaryArticle.prototype.isPage = function() {
};
/**
+ * Check if this article is a file (exatcly)
+ *
+ * @param {File} file
+ * @return {Boolean}
+ */
+SummaryArticle.prototype.isFile = function(file) {
+ return (
+ file.getPath() === this.getPath()
+ && this.getAnchor() === undefined
+ );
+};
+
+/**
+ * Check if this article is the introduction of the book
+ *
+ * @param {Book|Readme} book
+ * @return {Boolean}
+ */
+SummaryArticle.prototype.isReadme = function(book) {
+ var readme = book.getFile? book : book.getReadme();
+ var file = readme.getFile();
+
+ return this.isFile(file);
+};
+
+/**
* Is article pointing to aan absolute url
*
* @return {Boolean}
diff --git a/lib/output/getModifiers.js b/lib/output/getModifiers.js
index 66fbc1a..bb44e80 100644
--- a/lib/output/getModifiers.js
+++ b/lib/output/getModifiers.js
@@ -9,11 +9,11 @@ var fileToOutput = require('./helper/fileToOutput');
var CODEBLOCK = 'code';
/**
- Return default modifier to prepare a page for
- rendering.
-
- @return {Array<Modifier>}
-*/
+ * Return default modifier to prepare a page for
+ * rendering.
+ *
+ * @return {Array<Modifier>}
+ */
function getModifiers(output, page) {
var book = output.getBook();
var plugins = output.getPlugins();
diff --git a/lib/output/helper/fileToOutput.js b/lib/output/helper/fileToOutput.js
index 9673162..361c6eb 100644
--- a/lib/output/helper/fileToOutput.js
+++ b/lib/output/helper/fileToOutput.js
@@ -6,12 +6,12 @@ var LocationUtils = require('../../utils/location');
var OUTPUT_EXTENSION = '.html';
/**
- Convert a filePath (absolute) to a filename for output
-
- @param {Output} output
- @param {String} filePath
- @return {String}
-*/
+ * Convert a filePath (absolute) to a filename for output
+ *
+ * @param {Output} output
+ * @param {String} filePath
+ * @return {String}
+ */
function fileToOutput(output, filePath) {
var book = output.getBook();
var readme = book.getReadme();
diff --git a/lib/output/helper/resolveFileToURL.js b/lib/output/helper/resolveFileToURL.js
index 026b0e5..3f52713 100644
--- a/lib/output/helper/resolveFileToURL.js
+++ b/lib/output/helper/resolveFileToURL.js
@@ -3,12 +3,12 @@ var LocationUtils = require('../../utils/location');
var fileToURL = require('./fileToURL');
/**
- Resolve an absolute path (extracted from a link)
-
- @param {Output} output
- @param {String} filePath
- @return {String}
-*/
+ * Resolve an absolute path (extracted from a link)
+ *
+ * @param {Output} output
+ * @param {String} filePath
+ * @return {String}
+ */
function resolveFileToURL(output, filePath) {
// Convert /test.png -> test.png
filePath = LocationUtils.toAbsolute(filePath, '', '');
diff --git a/lib/output/modifiers/annotateText.js b/lib/output/modifiers/annotateText.js
index 2b4b439..490c228 100644
--- a/lib/output/modifiers/annotateText.js
+++ b/lib/output/modifiers/annotateText.js
@@ -59,19 +59,18 @@ function replaceText($, el, search, replace, text_only ) {
}
/**
- Annotate text using a list of GlossaryEntry
-
- @param {List<GlossaryEntry>}
- @param {String} glossaryFilePath
- @param {HTMLDom} $
-*/
+ * Annotate text using a list of GlossaryEntry
+ *
+ * @param {List<GlossaryEntry>}
+ * @param {String} glossaryFilePath
+ * @param {HTMLDom} $
+ */
function annotateText(entries, glossaryFilePath, $) {
entries.forEach(function(entry) {
- var entryId = entry.getID();
- var name = entry.getName();
+ var entryId = entry.getID();
+ var name = entry.getName();
var description = entry.getDescription();
-
- var searchRegex = new RegExp( '\\b(' + pregQuote(name.toLowerCase()) + ')\\b' , 'gi' );
+ var searchRegex = new RegExp( '\\b(' + pregQuote(name.toLowerCase()) + ')\\b' , 'gi' );
$('*').each(function() {
var $this = $(this);
diff --git a/lib/utils/location.js b/lib/utils/location.js
index 17edc00..00d8004 100644
--- a/lib/utils/location.js
+++ b/lib/utils/location.js
@@ -40,13 +40,28 @@ function normalize(s) {
}
/**
- Convert a relative path to absolute
+ * Flatten a path, it removes the leading "/"
+ *
+ * @param {String} href
+ * @return {String}
+ */
+function flatten(href) {
+ href = normalize(href);
+ if (href[0] == '/') {
+ href = normalize(href.slice(1));
+ }
+
+ return href;
+}
- @param {String} href
- @param {String} dir: directory parent of the file currently in rendering process
- @param {String} outdir: directory parent from the html output
- @return {String}
-*/
+/**
+ * Convert a relative path to absolute
+ *
+ * @param {String} href
+ * @param {String} dir: directory parent of the file currently in rendering process
+ * @param {String} outdir: directory parent from the html output
+ * @return {String}
+ */
function toAbsolute(_href, dir, outdir) {
if (isExternal(_href) || isDataURI(_href)) {
return _href;
@@ -74,50 +89,51 @@ function toAbsolute(_href, dir, outdir) {
}
/**
- Convert an absolute path to a relative path for a specific folder (dir)
- ('test/', 'hello.md') -> '../hello.md'
-
- @param {String} dir: current directory
- @param {String} file: absolute path of file
- @return {String}
-*/
+ * Convert an absolute path to a relative path for a specific folder (dir)
+ * ('test/', 'hello.md') -> '../hello.md'
+ *
+ * @param {String} dir: current directory
+ * @param {String} file: absolute path of file
+ * @return {String}
+ */
function relative(dir, file) {
var isDirectory = file.slice(-1) === '/';
return normalize(path.relative(dir, file)) + (isDirectory? '/': '');
}
/**
- Convert an absolute path to a relative path for a specific folder (dir)
- ('test/test.md', 'hello.md') -> '../hello.md'
-
- @param {String} baseFile: current file
- @param {String} file: absolute path of file
- @return {String}
-*/
+ * Convert an absolute path to a relative path for a specific folder (dir)
+ * ('test/test.md', 'hello.md') -> '../hello.md'
+ *
+ * @param {String} baseFile: current file
+ * @param {String} file: absolute path of file
+ * @return {String}
+ */
function relativeForFile(baseFile, file) {
return relative(path.dirname(baseFile), file);
}
/**
- Compare two paths, return true if they are identical
- ('README.md', './README.md') -> true
-
- @param {String} p1: first path
- @param {String} p2: second path
- @return {Boolean}
-*/
+ * Compare two paths, return true if they are identical
+ * ('README.md', './README.md') -> true
+ *
+ * @param {String} p1: first path
+ * @param {String} p2: second path
+ * @return {Boolean}
+ */
function areIdenticalPaths(p1, p2) {
return normalize(p1) === normalize(p2);
}
module.exports = {
areIdenticalPaths: areIdenticalPaths,
- isDataURI: isDataURI,
- isExternal: isExternal,
- isRelative: isRelative,
- isAnchor: isAnchor,
- normalize: normalize,
- toAbsolute: toAbsolute,
- relative: relative,
- relativeForFile: relativeForFile
+ isDataURI: isDataURI,
+ isExternal: isExternal,
+ isRelative: isRelative,
+ isAnchor: isAnchor,
+ normalize: normalize,
+ toAbsolute: toAbsolute,
+ relative: relative,
+ relativeForFile: relativeForFile,
+ flatten: flatten
};