Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
www
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PLN
www
Commits
35824fea
Commit
35824fea
authored
Apr 25, 2021
by
PLN (Algolia)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lib: Genericize content
parent
5d9493aa
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
97 additions
and
75 deletions
+97
-75
hello-world.md
next/content/posts/hello-world.md
+0
-0
next.md
next/content/posts/next.md
+0
-0
posts.js
next/lib/posts.js
+4
-75
utils.js
next/lib/utils.js
+89
-0
styles.css
next/styles.css
+4
-0
No files found.
next/posts/hello-world.md
→
next/
content/
posts/hello-world.md
View file @
35824fea
File moved
next/posts/next.md
→
next/
content/
posts/next.md
View file @
35824fea
File moved
next/lib/posts.js
View file @
35824fea
import
fs
from
'fs'
import
path
from
'path'
import
matter
from
'gray-matter'
import
remark
from
'remark'
import
html
from
'remark-html'
const
postsDirectory
=
path
.
join
(
process
.
cwd
(),
'posts'
)
import
{
getAllContentData
,
getAllContentIds
,
getContentData
}
from
'./utils'
export
function
getSortedPostsData
()
{
// Get file names under /posts
const
fileNames
=
fs
.
readdirSync
(
postsDirectory
)
const
allPostsData
=
fileNames
.
map
(
fileName
=>
{
// Remove ".md" from file name to get id
const
id
=
fileName
.
replace
(
/
\.
md$/
,
''
)
// Read markdown file as string
const
fullPath
=
path
.
join
(
postsDirectory
,
fileName
)
const
fileContents
=
fs
.
readFileSync
(
fullPath
,
'utf8'
)
// Use gray-matter to parse the post metadata section
const
matterResult
=
matter
(
fileContents
)
// Combine the data with the id
return
{
id
,
...
matterResult
.
data
}
})
// Sort posts by date
return
allPostsData
.
sort
((
a
,
b
)
=>
{
if
(
a
.
date
<
b
.
date
)
{
return
1
}
else
{
return
-
1
}
})
return
getAllContentData
(
'posts'
,
true
)
}
export
function
getAllPostIds
()
{
const
fileNames
=
fs
.
readdirSync
(
postsDirectory
)
// Returns an array that looks like this:
// [
// {
// params: {
// id: 'ssg-ssr'
// }
// },
// {
// params: {
// id: 'pre-rendering'
// }
// }
// ]
// MUST be an array of objects
return
fileNames
.
map
(
fileName
=>
{
return
{
params
:
{
id
:
fileName
.
replace
(
/
\.
md$/
,
''
)
}
}
})
return
getAllContentIds
(
"posts"
)
}
export
async
function
getPostData
(
id
)
{
const
fullPath
=
path
.
join
(
postsDirectory
,
`
${
id
}
.md`
)
const
fileContents
=
fs
.
readFileSync
(
fullPath
,
'utf8'
)
// Use gray-matter to parse the post metadata section
const
matterResult
=
matter
(
fileContents
)
// Use remark to convert markdown into HTML string
const
processedContent
=
await
remark
()
.
use
(
html
)
.
process
(
matterResult
.
content
)
const
contentHtml
=
processedContent
.
toString
()
// Combine the data with the id and contentHtml
return
{
id
,
contentHtml
,
...
matterResult
.
data
}
return
getContentData
(
"posts"
,
id
)
}
next/lib/utils.js
0 → 100644
View file @
35824fea
import
path
from
"path"
;
import
fs
from
"fs"
;
import
matter
from
"gray-matter"
;
import
remark
from
"remark"
;
import
html
from
"remark-html"
;
function
getContentDirectory
(
name
)
{
const
dir
=
path
.
join
(
process
.
cwd
(),
"content"
,
name
);
console
.
log
(
"gCD:"
,
dir
);
return
dir
;
}
function
getAllContentData
(
name
,
sorted
=
false
)
{
// Get file names under /content/{name}
const
contentDirectory
=
getContentDirectory
(
name
);
const
fileNames
=
fs
.
readdirSync
(
contentDirectory
);
console
.
log
(
"gACD: total"
,
fileNames
.
length
,
"items"
);
const
allContentData
=
fileNames
.
map
((
fileName
)
=>
{
// Remove ".md" from file name to get id
const
id
=
fileName
.
replace
(
/
\.
md$/
,
""
);
// Read markdown file as string
const
fullPath
=
path
.
join
(
contentDirectory
,
fileName
);
const
fileContents
=
fs
.
readFileSync
(
fullPath
,
"utf8"
);
// Use gray-matter to parse the post metadata section
const
matterResult
=
matter
(
fileContents
);
// Combine the data with the id
return
{
id
,
...
matterResult
.
data
,
};
});
return
sorted
?
allContentData
.
sort
((
a
,
b
)
=>
{
if
(
a
.
date
<
b
.
date
)
{
return
1
;
}
else
{
return
-
1
;
}
})
:
allContentData
;
}
function
getAllContentIds
(
name
)
{
const
fileNames
=
fs
.
readdirSync
(
getContentDirectory
(
name
));
console
.
log
(
`getAllContentIds:
${
fileNames
.
length
}
${
name
}
(s).`
);
return
fileNames
.
map
((
fileName
)
=>
{
return
{
params
:
{
id
:
fileName
.
replace
(
/
\.
md$/
,
""
),
},
};
});
}
async
function
getContentData
(
name
,
id
)
{
const
fullPath
=
path
.
join
(
getContentDirectory
(
name
),
`
${
id
}
.md`
);
console
.
log
(
"Got content fullPath:"
,
fullPath
);
const
fileContents
=
fs
.
readFileSync
(
fullPath
,
"utf8"
);
console
.
log
(
"Got contents:"
,
fileContents
);
// Use gray-matter to parse the post metadata section
const
matterResult
=
matter
(
fileContents
);
// Use remark to convert markdown into HTML string
const
processedContent
=
await
remark
()
.
use
(
html
)
.
process
(
matterResult
.
content
);
const
contentHtml
=
processedContent
.
toString
();
// Combine the data with the id and contentHtml
return
{
id
,
contentHtml
,
...
matterResult
.
data
,
};
}
export
{
getContentDirectory
,
getContentData
,
getAllContentData
,
getAllContentIds
,
};
next/styles.css
View file @
35824fea
...
...
@@ -30,6 +30,10 @@ header {
margin-bottom
:
2em
;
}
main
h1
{
margin-bottom
:
1em
;
}
footer
{
margin-top
:
4em
;
text-align
:
center
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment