-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: getters for title and summary (#12)
* feat: getters for title and summary * chore: increase default summary length to 150 * chore: use truncate * chore: rename length to truncate * chore: remove default truncate * feat: add getTags getter * chore: fix comment * chore: refactor into getProperty func
- Loading branch information
Showing
18 changed files
with
1,657 additions
and
871 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,51 @@ | ||
import { parse } from '../../parse'; | ||
import { getSummary } from '../get-summary'; | ||
|
||
describe('get-summary', () => { | ||
it('should return frontmatter summary if present', () => { | ||
const summary = getSummary( | ||
parse(`--- | ||
summary: my summary | ||
--- | ||
# Other Title | ||
Yo | ||
`), | ||
); | ||
|
||
expect(summary).toBe('my summary'); | ||
}); | ||
|
||
it('should return first paragraph if present', () => { | ||
const summary = getSummary( | ||
parse(`# Other Title | ||
Paragraph 1. | ||
Paragraph 2. | ||
`), | ||
); | ||
|
||
expect(summary).toBe('Paragraph 1.'); | ||
}); | ||
|
||
it('should strip markdown out of paragraph', () => { | ||
const summary = getSummary(parse(`this has a [link](./foo.json)`)); | ||
|
||
expect(summary).toBe('this has a link'); | ||
}); | ||
|
||
it('should accept a length option', () => { | ||
const summary = getSummary(parse(`the dog barks`), { | ||
truncate: 3, | ||
}); | ||
|
||
expect(summary).toBe('the...'); | ||
}); | ||
|
||
it('should return undefined if no frontmatter summary and no paragraphs', () => { | ||
const summary = getSummary(parse(`## Hello`)); | ||
expect(summary).toBe(undefined); | ||
}); | ||
}); |
This file contains 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,49 @@ | ||
import { parse } from '../../parse'; | ||
import { getTags } from '../get-tags'; | ||
|
||
describe('get-tags', () => { | ||
it('should return frontmatter tags if present', () => { | ||
const tags = getTags( | ||
parse(`--- | ||
tags: [t1, t2] | ||
--- | ||
# Other Title | ||
Yo | ||
`), | ||
); | ||
|
||
expect(tags).toEqual(['t1', 't2']); | ||
}); | ||
|
||
it('should remove nil tags', () => { | ||
const tags = getTags( | ||
parse(`--- | ||
tags: [t1, undefined] | ||
---`), | ||
); | ||
|
||
expect(tags).toEqual(['t1']); | ||
}); | ||
|
||
it('should not fail if tags is undefined', () => { | ||
const tags = getTags( | ||
parse(`--- | ||
tags: undefined | ||
---`), | ||
); | ||
|
||
expect(tags).toEqual([]); | ||
}); | ||
|
||
it('should not fail if tags is null', () => { | ||
const tags = getTags( | ||
parse(`--- | ||
tags: null | ||
---`), | ||
); | ||
|
||
expect(tags).toEqual([]); | ||
}); | ||
}); |
This file contains 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,52 @@ | ||
import { parse } from '../../parse'; | ||
import { getTitle } from '../get-title'; | ||
|
||
describe('get-title', () => { | ||
it('should return frontmatter title if present', () => { | ||
const title = getTitle( | ||
parse(`--- | ||
title: hello | ||
--- | ||
# Other Title | ||
Yo | ||
`), | ||
); | ||
|
||
expect(title).toBe('hello'); | ||
}); | ||
|
||
it('should return heading if present', () => { | ||
const title = getTitle( | ||
parse(`--- | ||
summary: what it is about | ||
--- | ||
# Other Title | ||
Yo | ||
`), | ||
); | ||
|
||
expect(title).toBe('Other Title'); | ||
}); | ||
|
||
it('should return headings other than h1 if present', () => { | ||
const title = getTitle( | ||
parse(`what it is about | ||
## Other Title 2 | ||
Yo | ||
`), | ||
); | ||
|
||
expect(title).toBe('Other Title 2'); | ||
}); | ||
|
||
it('should return undefined if no frontmatter title and no headings', () => { | ||
const title = getTitle(parse(`what it is about`)); | ||
expect(title).toBe(undefined); | ||
}); | ||
}); |
This file contains 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,29 @@ | ||
import * as Unist from 'unist'; | ||
|
||
const { select } = require('unist-util-select'); | ||
const toString = require('mdast-util-to-string'); | ||
|
||
import { Frontmatter } from '../frontmatter'; | ||
|
||
// Priority: yaml title, then first heading | ||
export const getProperty = (propName: string, element?: string, data?: Unist.Node) => { | ||
let target: string | void | undefined; | ||
|
||
if (data) { | ||
try { | ||
const frontmatter = new Frontmatter(data, true); | ||
target = frontmatter.get(propName); | ||
|
||
if (element && !target) { | ||
const elem = select(element, data); | ||
if (elem) { | ||
target = toString(elem); | ||
} | ||
} | ||
} catch (e) { | ||
console.warn(`Error getting ${propName} from markdown document`, e); | ||
} | ||
} | ||
|
||
return target; | ||
}; |
This file contains 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,20 @@ | ||
import { truncate } from 'lodash'; | ||
import * as Unist from 'unist'; | ||
|
||
import { getProperty } from './get-property'; | ||
|
||
export interface IGetSummaryOpts { | ||
truncate?: number; | ||
} | ||
|
||
// Priority: yaml summary, then first paragraph | ||
export const getSummary = (data?: Unist.Node, opts: IGetSummaryOpts = {}) => { | ||
let summary = getProperty('summary', 'paragraph', data); | ||
|
||
if (summary && opts.truncate) { | ||
// +3 to account for ellipsis | ||
summary = truncate(summary, { length: opts.truncate + 3 }); | ||
} | ||
|
||
return summary; | ||
}; |
This file contains 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,29 @@ | ||
import * as Unist from 'unist'; | ||
|
||
import { Frontmatter } from '../frontmatter'; | ||
|
||
// Priority: yaml tags | ||
export const getTags = (data?: Unist.Node): string[] => { | ||
const tags: string[] = []; | ||
|
||
if (data) { | ||
try { | ||
const frontmatter = new Frontmatter(data, true); | ||
const dataTags = frontmatter.get('tags'); | ||
|
||
if (dataTags && Array.isArray(dataTags)) { | ||
return dataTags.reduce<string[]>((filteredTags, tag) => { | ||
if (tag && typeof tag === 'string' && tag !== 'undefined' && tag !== 'null') { | ||
filteredTags.push(String(tag)); | ||
} | ||
|
||
return filteredTags; | ||
}, []); | ||
} | ||
} catch (e) { | ||
console.warn('Error getting tags from markdown document', e); | ||
} | ||
} | ||
|
||
return tags; | ||
}; |
This file contains 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,8 @@ | ||
import * as Unist from 'unist'; | ||
|
||
import { getProperty } from './get-property'; | ||
|
||
// Priority: yaml title, then first heading | ||
export const getTitle = (data?: Unist.Node) => { | ||
return getProperty('title', 'heading', data); | ||
}; |
This file contains 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,4 @@ | ||
export * from './get-summary'; | ||
export * from './get-tags'; | ||
export * from './get-title'; | ||
export * from './get-property'; |
This file contains 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 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 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 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
Oops, something went wrong.