Skip to content

Commit

Permalink
allow passing filename query param to get snippet (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix22 committed May 15, 2020
1 parent ba22912 commit 042a20b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
7 changes: 5 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ const unsplash = {
},
}

function getSnippet(uid = '', { host } = {}) {
function getSnippet(uid = '', { host, filename } = {}) {
return client
.get(`/snippets/${uid}`, host ? { baseURL: `https://${host}/api` } : undefined)
.get(`/snippets/${uid}`, {
baseURL: host ? `https://${host}/api` : undefined,
params: { filename },
})
.then(res => res.data)
.catch(e => {
console.error(e)
Expand Down
4 changes: 2 additions & 2 deletions pages/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import IndexPage from './index'
import api from '../lib/api'

export async function getServerSideProps({ req, res, query }) {
const path = query.id
const { id: path, filename } = query
const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null

let snippet
if (parameter) {
const host = req ? req.headers.host : undefined
snippet = await api.snippet.get(parameter, { host })
snippet = await api.snippet.get(parameter, { host, filename })
if (snippet) {
return { props: { snippet, host } }
}
Expand Down
5 changes: 3 additions & 2 deletions pages/embed/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import EmbedPage from './index'
import api from '../../lib/api'

export async function getServerSideProps({ req, res, query }) {
const path = query.id
const { id: path, filename } = query

const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null

let snippet
if (parameter) {
const host = req ? req.headers.host : undefined
snippet = await api.snippet.get(parameter, { host })
snippet = await api.snippet.get(parameter, { host, filename })
if (snippet /* && snippet.gist_id */) {
return { props: { snippet } }
}
Expand Down

0 comments on commit 042a20b

Please sign in to comment.