Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

chore: use gray matter for excerpt feature #169

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { join } = require('path')
const loaderUtils = require('loader-utils')
const uppercamelcase = require('uppercamelcase')
const paramCase = require('param-case')
const fm = require('front-matter')
const fm = require('gray-matter')

const getDirOpts = (contentOptions, section) => {
// configuration options can be for root files ('/') but regex for section also
Expand Down Expand Up @@ -114,7 +114,7 @@ module.exports = function nuxtent(source) {
const section = getSection(this.context)
const dirOpts = getDirOpts(content, section)

const { body } = fm(source)
const { content: body } = fm(source)
const { transformedSource, components } = mdComponents(
body,
componentsDir,
Expand Down
2 changes: 1 addition & 1 deletion dist/module.js

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions lib/content/page.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'path'
import { readFileSync, statSync } from 'fs'

import fm from 'front-matter'
import fm from 'gray-matter'
import dateFns from 'date-fns'
import paramCase from 'param-case'
import pathToRegexp from 'path-to-regexp'
Expand Down Expand Up @@ -35,7 +35,8 @@ export default function prepPage(meta, options, isDev) {
'permalink',
'anchors',
'attributes',
'body'
'body',
'excerpt'
])

if (params.exclude) {
Expand Down Expand Up @@ -136,6 +137,10 @@ export default function prepPage(meta, options, isDev) {
return this._rawData.attributes
},

get excerpt() {
return this._rawData.excerpt
},

get body() {
if (isDev || !cached.body) {
const { _rawData } = this
Expand Down Expand Up @@ -179,8 +184,12 @@ export default function prepPage(meta, options, isDev) {
if (isDev || !cached.data) {
const source = readFileSync(meta.filePath).toString()
if (meta.fileName.search(/\.md$/) > -1) {
const { attributes, body } = fm(source)
cached.data = { attributes, body }
const separator = options.excerpt
const { data: attributes, content: body, excerpt } = fm(source, {
excerpt: Boolean(separator),
excerpt_separator: String(separator)
})
cached.data = { attributes, body, excerpt }
} else if (meta.fileName.search(/\.(yaml|yml)$/) > -1) {
cached.data = { attributes: {}, body: source }
}
Expand Down
4 changes: 2 additions & 2 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { join } = require('path')
const loaderUtils = require('loader-utils')
const uppercamelcase = require('uppercamelcase')
const paramCase = require('param-case')
const fm = require('front-matter')
const fm = require('gray-matter')

const getDirOpts = (contentOptions, section) => {
// configuration options can be for root files ('/') but regex for section also
Expand Down Expand Up @@ -114,7 +114,7 @@ module.exports = function nuxtent(source) {
const section = getSection(this.context)
const dirOpts = getDirOpts(content, section)

const { body } = fm(source)
const { content: body } = fm(source)
const { transformedSource, components } = mdComponents(
body,
componentsDir,
Expand Down
1 change: 1 addition & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default function ContentModule(moduleOpts) {
nuxtentConfig(this.options.rootDir) || this.options.nuxtent || {}

const content = mergeContentOptions(userOptions.content, {
excerpt: moduleOpts.excerpt,
page: null,
permalink: ':slug',
anchorsLevel: 1,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"chalk": "^2.3.0",
"date-fns": "^1.28.5",
"front-matter": "^2.3.0",
"gray-matter": "^4.0.1",
"js-yaml": "^3.10.0",
"loader-utils": "^1.1.0",
"markdown-it": "^8.4.0",
Expand Down
33 changes: 27 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3831,12 +3831,6 @@ from2@^2.1.0:
inherits "^2.0.1"
readable-stream "^2.0.0"

front-matter@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/front-matter/-/front-matter-2.3.0.tgz#7203af896ce357ee04e2aa45169ea91ed7f67504"
dependencies:
js-yaml "^3.10.0"

fs-access@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/fs-access/-/fs-access-1.0.1.tgz#d6a87f262271cefebec30c553407fb995da8777a"
Expand Down Expand Up @@ -4147,6 +4141,15 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6,
version "1.0.1"
resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"

gray-matter@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-4.0.1.tgz#375263c194f0d9755578c277e41b1c1dfdf22c7d"
dependencies:
js-yaml "^3.11.0"
kind-of "^6.0.2"
section-matter "^1.0.0"
strip-bom-string "^1.0.0"

[email protected]:
version "1.10.3"
resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.3.tgz#1926ba90cf3edfe2adb4927f5880bc22c66c790f"
Expand Down Expand Up @@ -5308,6 +5311,13 @@ js-yaml@^3.10.0, js-yaml@^3.3.1, js-yaml@^3.4.3, js-yaml@^3.7.0, js-yaml@^3.9.0,
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^3.11.0:
version "3.11.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.11.0.tgz#597c1a8bd57152f26d622ce4117851a51f5ebaef"
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@~3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80"
Expand Down Expand Up @@ -8665,6 +8675,13 @@ schema-utils@^0.4.0, schema-utils@^0.4.5:
ajv "^6.1.0"
ajv-keywords "^3.1.0"

section-matter@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/section-matter/-/section-matter-1.0.0.tgz#e9041953506780ec01d59f292a19c7b850b84167"
dependencies:
extend-shallow "^2.0.1"
kind-of "^6.0.0"

semantic-release-cli@^3.0.3:
version "3.2.2"
resolved "https://registry.yarnpkg.com/semantic-release-cli/-/semantic-release-cli-3.2.2.tgz#1c3b48c97fdc4f0a931ee23476ea156d9ba4cff3"
Expand Down Expand Up @@ -9244,6 +9261,10 @@ strip-ansi@^4.0.0, strip-ansi@~4.0.0:
dependencies:
ansi-regex "^3.0.0"

strip-bom-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/strip-bom-string/-/strip-bom-string-1.0.0.tgz#e5211e9224369fbb81d633a2f00044dc8cedad92"

[email protected], strip-bom@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
Expand Down