Skip to content
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

add search data to component and add doc pages (closes #84) #104

Merged
merged 1 commit into from
May 16, 2020
Merged
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
64 changes: 0 additions & 64 deletions gridsome.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,7 @@
// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`

const fs = require('fs');
const path = require('path');
const pick = require('lodash.pick');
const { pathPrefix } = require('./gridsome.config')

module.exports = function (api, options) {
api.loadSource(store => {
/*
Clean the pathPrefix
====================
not used => '/'
'' => '/'
'/' => '/'
'/path' => '/path'
'path' => '/path'
'path/' => '/path'
'/path/' => '/path'
*/
const cleanedPathPrefix = `${pathPrefix ? ['', ...pathPrefix.split('/').filter(dir=>dir.length)].join('/') : ''}`

/*
Query
=====
<static-query> <!-- or a page-query -->
{
metaData{
pathPrefix
}
}
</static-query>

Requests for static files should look like this:
===============================================
Using static-queries: axios( this.$static.metaData.pathPrefix + "/fileName" )
Using page-queries, axios( this.$page.metaData.pathPrefix + "/fileName" )
*/
store.addMetadata('pathPrefix', cleanedPathPrefix)
})

api.beforeBuild(({ config, store }) => {

// Generate an index file for Fuse to search Posts
const { collection } = store.getContentType('Post');

const posts = collection.data.map(post => {
return pick(post, ['title', 'path', 'summary']);
});

const output = {
dir: './static',
name: 'search.json',
...options.output
}

const outputPath = path.resolve(process.cwd(), output.dir)
const outputPathExists = fs.existsSync(outputPath)
const fileName = output.name.endsWith('.json')
? output.name
: `${output.name}.json`

if (outputPathExists) {
fs.writeFileSync(path.resolve(process.cwd(), output.dir, fileName), JSON.stringify(posts))
} else {
fs.mkdirSync(outputPath)
fs.writeFileSync(path.resolve(process.cwd(), output.dir, fileName), JSON.stringify(posts))
}
})
}
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"gridsome": "^0.7.14",
"gridsome-plugin-remark-shiki": "^0.3.1",
"gridsome-plugin-rss": "^1.2.0",
"lodash.pick": "^4.4.0",
"vue-fuse": "^2.2.1",
"vue-scrollto": "^2.18.1"
},
Expand Down
58 changes: 45 additions & 13 deletions src/components/SearchInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,66 @@


<static-query>
{
metadata{
pathPrefix
query Search {
allPost {
edges {
node {
id
path
title
summary
headings {
depth
value
anchor
}
}
}
}
allDocumentation {
edges {
node {
id
path
title
}
}
}
}
</static-query>

<script>
import axios from 'axios'
import SearchFocus from './SearchFocus'

export default {
components: {
SearchFocus,
},
created() {
axios(this.$static.metadata.pathPrefix + "/search.json").then(response => {
this.posts = response.data
})
.catch(error => {
console.log(error);
})
computed: {
pages () {
let result = [];
const allPost = this.$static.allPost.edges.map(edge => edge.node);
allPost.forEach(page => {
result.push({
path: page.path,
title: page.title,
summary: page.summary
});
});
const allDocs = this.$static.allDocumentation.edges.map(edge => edge.node);
allDocs.forEach(page => {
result.push({
path: page.path,
title: page.title
});
});
return result;
}
},
data() {
return {
query: '',
results: [],
posts: [],
highlightedIndex: 0,
searchResultsVisible: false,
options: {
Expand All @@ -109,7 +141,7 @@ export default {
this.searchResultsVisible = true
},
performSearch() {
this.$search(this.query, this.posts, this.options).then(results => {
this.$search(this.query, this.pages, this.options).then(results => {
this.results = results
})
},
Expand Down