-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Resolved reformat of blog posts #232
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
52a87d0
Added basic helper
fbaiodias 9096e05
broken summary of first para from blog
elinnet d2c4f13
Looks like it's working
fbaiodias afb299a
Cheerio as dep and fixed style issues for blog reformatting
phillipj 8399dcf
Implemented blog post pagination.
phillipj 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 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 |
---|---|---|
@@ -1,4 +1,11 @@ | ||
.blog-index | ||
list-style none | ||
padding 0 | ||
|
||
time | ||
margin-right 1em | ||
color $light-gray | ||
|
||
.summary | ||
margin-left 1em | ||
font-size: 75% |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--- | ||
layout: blog-index.hbs | ||
paginate: blog | ||
--- |
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,47 @@ | ||
'use strict' | ||
|
||
const cheerio = require('cheerio') | ||
|
||
const SUMMARY_MAX_LENGTH = 300 | ||
const IGNORE_SELECTORS = ['.blogpost-header', '.anchor', 'h1', 'h2', 'h3', 'blockquote'] | ||
|
||
/** | ||
* Due to the nature of metalsmith and | ||
* how the metalsmith-paginate plugin operates, | ||
* this helper has to handle two different types of | ||
* HTML contents: | ||
* - clean blog posts converted from markdown to HTML, | ||
* seen on the first page of blog posts | ||
* - the remaining paginated pages has gone | ||
* through the handlebars process and therefore has the | ||
* entire page layout (w/<html>, <head> and <body> etc) | ||
* wrapped around the actual blog contents :( | ||
*/ | ||
|
||
module.exports = function (contents, locale, path) { | ||
const $ = cheerio.load(contents) | ||
const $body = $('body') | ||
const hasBody = $body.length > 0 | ||
const $elements = hasBody ? $body.find('article > *') : $('*') | ||
|
||
let summary = '' | ||
|
||
$elements | ||
.not((i, elem) => IGNORE_SELECTORS.some((selector) => $(elem).is(selector))) | ||
.each((i, elem) => { | ||
if (summary.length > SUMMARY_MAX_LENGTH) { | ||
summary += `<p><a href='/${locale}/${path}/'>Read more...</a></p>` | ||
return false | ||
} | ||
|
||
// dont re-add nested elements when extracting summary | ||
// from blog posts not contained in a complete HTML document | ||
if (!hasBody && elem.parent) { | ||
return | ||
} | ||
|
||
summary += $.html(elem) | ||
}) | ||
|
||
return summary | ||
} |
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.
This sums up my hours of trial'n'error to get this working.