front-matter
Advanced tools
Comparing version 3.0.2 to 3.1.0
interface FrontMatterResult<T> { | ||
readonly attributes: T | ||
readonly body: string | ||
readonly bodyBegin: number; | ||
readonly frontmatter?: string | ||
@@ -5,0 +6,0 @@ } |
34
index.js
@@ -26,13 +26,33 @@ var parser = require('js-yaml') | ||
} else { | ||
return { attributes: {}, body: string } | ||
return { | ||
attributes: {}, | ||
body: string, | ||
bodyBegin: 1 | ||
} | ||
} | ||
} | ||
function computeLocation (match, body) { | ||
var line = 1 | ||
var pos = body.indexOf('\n') | ||
var offset = match.index + match[0].length | ||
while (pos !== -1) { | ||
if (pos >= offset) { | ||
return line | ||
} | ||
line++ | ||
pos = body.indexOf('\n', pos + 1) | ||
} | ||
return line | ||
} | ||
function parse (string) { | ||
var match = regex.exec(string) | ||
if (!match) { | ||
return { | ||
attributes: {}, | ||
body: string | ||
body: string, | ||
bodyBegin: 1 | ||
} | ||
@@ -44,4 +64,10 @@ } | ||
var body = string.replace(match[0], '') | ||
var line = computeLocation(match, string) | ||
return { attributes: attributes, body: body, frontmatter: yaml } | ||
return { | ||
attributes: attributes, | ||
body: body, | ||
bodyBegin: line, | ||
frontmatter: yaml | ||
} | ||
} | ||
@@ -48,0 +74,0 @@ |
@@ -11,3 +11,3 @@ { | ||
"license": "MIT", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"homepage": "https://github.com/jxson/front-matter", | ||
@@ -27,4 +27,4 @@ "bugs": "https://github.com/jxson/front-matter/issues", | ||
"devDependencies": { | ||
"brfs": "^1.4.3", | ||
"coveralls": "^2.11.6", | ||
"brfs": "^2.0.2", | ||
"coveralls": "^3.0.9", | ||
"istanbul": "^0.4.5", | ||
@@ -31,0 +31,0 @@ "standard": "^6.0.4", |
@@ -63,3 +63,4 @@ # front-matter | ||
}, | ||
body: '\nThis is some text about some stuff that happened sometime ago', | ||
body: 'This is some text about some stuff that happened sometime ago', | ||
bodyBegin: 6, | ||
frontmatter: 'title: Just hack\'n\ndescription: Nothing to see here' | ||
@@ -81,2 +82,3 @@ } | ||
* `content.body` contains the string contents below the yaml separators | ||
* `content.bodyBegin` contains the line number the body contents begins at | ||
* `content.frontmatter` contains the original yaml string contents | ||
@@ -83,0 +85,0 @@ |
@@ -30,2 +30,4 @@ var fm = require('../') | ||
t.equal(content.bodyBegin, 10) | ||
t.ok(content.frontmatter, 'should have a `frontmatter` key') | ||
@@ -98,2 +100,3 @@ t.ok(content.frontmatter.match('title: Three dashes marks the spot'), 'should match frontmatter') | ||
t.equal(content.body, '') | ||
t.equal(content.bodyBegin, 9) | ||
t.end() | ||
@@ -151,2 +154,3 @@ }) | ||
t.equal(content.body, data) | ||
t.equal(content.bodyBegin, 1) | ||
t.end() | ||
@@ -218,3 +222,4 @@ }) | ||
attributes: {}, | ||
body: string | ||
body: string, | ||
bodyBegin: 1 | ||
}) | ||
@@ -227,2 +232,3 @@ | ||
body: '', | ||
bodyBegin: 4, | ||
frontmatter: 'foo: bar' | ||
@@ -229,0 +235,0 @@ }) |
20753
269
160