Calculate the similarity between posts and make it available from graphql.
To calculate the similarity, this plugin using tf-idf and Cosine similarity.
Installation
npm i --save gatsby-remark-related-posts
Usage
In your gatsby-config.js
:
{
resolve: "gatsby-remark-related-posts",
options: {
posts_dir: `${__dirname}/posts`,
doc_lang: "ja",
},
},
option | description |
---|
posts_dir | directory that includes your markdown files. |
doc_lang | ISO 639-1 language code of your post. This supports en and ja currently. |
The plugin creates a new relatedMarkdownRemarkNodes
field on each MarkdownRemark
node, like this:
{
allMarkdownRemark {
nodes {
frontmatter {
title
}
fields {
relatedMarkdownRemarks {
frontmatter {
title
}
}
}
}
}
}
{
"data": {
"allMarkdownRemark": {
"nodes": [
{
"frontmatter": {
"title": "New Beginnings"
},
"fields": {
"relatedMarkdownRemarks": [
{
"frontmatter": {
"title": "Hello World"
}
},
{
"frontmatter": {
"title": "My Second Post!"
}
}
]
}
},
...
These MarkdownRemark nodes are sorting by similarity. In this example, first "Hello World" post is the most related to "New Beginnings" post.
see also: sample repo
Licence
MIT