νμ΄μ§ μ νλ³λ‘ μ¬λ¬ ν΄λκ° μμ΅λλ€. μ¦. λ§ν¬ λ€μ΄ νμΌμ ν¬ν¨νλ type1, type2 λ±.
--type1
--type2
κ° ν΄λμ νμΌμ gatsby-transformer-remark
μ¬μ©νμ¬ λ³νλ©λλ€.
type1, type2 λ±μ λν μ½ν
μΈ λ§ μΏΌλ¦¬νκ³ μΆμ΅λλ€.
μλ₯Ό λ€μ΄ λͺ¨λ type1 νμ΄μ§μ λͺ©λ‘μ ν¬ν¨νλ type1 λͺ©λ‘ νμ΄μ§λ₯Ό μν©λλ€.
gatsby-config.js
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/src/pages/type1`,
name: 'type1',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/src/pages/type2`,
name: 'type2',
},
},
λ΄ κ°μ μ name
μμ±μ κΈ°λ°μΌλ‘ 쿼리 ν μ ββμλ€λ κ²μ
λλ€.
λ€μκ³Ό κ°μ κ² :
{
someQueryForGatsbySourceFilesystem(
filter: {
name: {
eq: "type1"
}
}
) {
edges {
node {
childMarkdownRemark {
html
}
}
}
}
}
allSite λ° allFileκ³Ό κ°μ λͺ¨λ λ€λ₯Έ 쿼리λ₯Ό μλνμ§λ§ μ루μ
μ μ°Ύμ μ μμ΅λλ€.
λλ λν μλνλ€
allSitePlugin(filter: {
pluginOptions: {
name: {
eq: "type1"
}
}
})
μ½ν μΈ λ₯Ό λ λλ§νλ λ° μ¬μ©ν μμλ κ²μ λ°ννμ§ μλ κ² κ°μ΅λλ€.
"Cannot query field \"childMarkdownRemark\" on type \"SitePlugin\".",
μ΄λ»κ² νμκ² μ΅λκΉ? κ²½λ‘ μ΄λ¦ (μ : type1
ν΄λ λ° λ§ν¬ λ€μ΄ νμ₯μμ μΌμΉνλ μ κ·μ)μ κΈ°λ°μΌλ‘ allFile λ° νν°λ₯Ό μ¬μ©ν©λκΉ?
allFile(
filter: {
absolutePath:{regex:"/(type1)\/.*\\.md$/"}
}
) {...}
allMarkdownRemark
μ¬μ©νμ¬ κ²½λ‘λ₯Ό νν°λ§ ν μλ μλ€κ³ μκ°νμ΅λλ€.
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date]},
filter: {fileAbsolutePath: {regex: "/(type1)/.*\\.md$/"}}
) {
edges {
node {
excerpt(pruneLength: 250)
id
frontmatter {
title
date(formatString: "MMMM DD, YYYY")
path
}
}
}
}
}
νμΌ λ
Έλμλ sourceInstanceName
λ‘ μ€μ λ μμ€ μΈμ€ν΄μ€ μ΄λ¦μ΄ μμΌλ―λ‘μ΄λ₯Ό 쿼리μ μ¬μ©ν μ μμ΅λλ€.
regexλ₯Ό μ¬μ©νλ κ²μ μ’μ μ루μ μ΄κΈ°λν©λλ€. μλ₯Ό λ€μ΄ gatsbyjs.orgμμ λ§μ΄ μ¬μ©ν©λλ€.
BTW, μμ§ μ¬μ©νκ³ μμ§ μλ€λ©΄ GraphiQLμ μ¬μ© κ°λ₯ν λ°μ΄ν°λ₯Ό νμνλ λ° μ ν©ν©λλ€.
@KyleAMathews allMarkdownRemark
쿼리μμ sourceInstanceName
λ₯Ό μ¬μ©νλ λ°©λ²μ΄ μμ΅λκΉ? μ΄μ²λΌ κ° λ§ν¬ λ€μ΄ λ
Έλμ λν νμΌ λ
Έλμ sourceInstanceName
λ₯Ό μ»μ μ μκΈ°λ₯Ό λ°λΌκ³ μμκΈ° λλ¬Έμ νμΌ κ²½λ‘μ μ κ·μμ μ¬μ©νμ§ μκ³ λ λ΄ νλ‘μ νΈμμ λΈλ‘κ·Έ κ²μλ¬Όμ λΆλ¦¬ ν μ ββμμ΅λλ€.
{
allMarkdownRemark {
edges {
node {
sourceInstanceName
}
}
}
}
κ·Έλ¬λ GraphQL λλ²κ±°λ sourceInstanceName
λ₯Ό μ¬μ©ν μ μμμ 보μ¬μ€λλ€.
νλ‘μ νΈ λ° λΈλ‘κ·Έ κ²μλ¬Όκ³Ό κ°μ μμ€ μ νλ³λ‘ λ§ν¬ λ€μ΄ νμ΄μ§λ₯Ό νν°λ§νλ κ²μ΄ μ’μ΅λλ€. μ΄μ λν λμ νμ¬ ν΄κ²°μ±
μ κ° λ§ν¬ λ€μ΄ λ
Έλμ fileAbsolutePath
λ₯Ό μ»λ κ²μ
λλ€.
{
allMarkdownRemark {
edges {
node {
fileAbsolutePath
}
}
}
}
κ·Έλ° λ€μ /pages/blog/
λλ /pages/projects/
μ κ°μ νΉμ λ¬Έμμ΄μ΄ ν¬ν¨λμ΄ μλμ§ ν
μ€νΈν©λλ€. κ·Έλ¬λ gatsby μ¬μ΄νΈ λλ ν 리 μ체μ λν μ λ κ²½λ‘μ μ΄λ¬ν λ¬Έμμ΄ μ€ νλκ° ν¬ν¨λμ΄ μμΌλ©΄ μ€λ¨λ©λλ€. μ΄ κ²½μ° λͺ¨λ² μ¬λ‘λ‘ λ¬΄μμ κΆμ₯ν©λκΉ? (λ¬Έμμμ λκ°λ₯Ό λμΉ κ²½μ° μ£μ‘ν©λλ€.)
@nwshane μ allFile
쿼리νκ³ κ±°κΈ°μμ νν°λ§ν©λλ€. internal.mediaType
λ‘ λ§ν¬ λ€μ΄ νμΌμ νν°λ§ ν μ μμ΅λλ€.
@KyleAMathews allFile
μ λν 쿼리λ₯Ό frontmatterμ λν μ λ ¬κ³Ό μ΄λ»κ² κ²°ν©ν©λκΉ? κ°λ₯ν©λκΉ?
λΉμ·ν κ²μ μμ λ΄λ €κ³ ν©λλ€.
νμ ν΄λλ‘ νν°λ§ ν μ μμ΅λλ€. νμ§λ§ frontmatter
μμ allFile
frontmatter
μ (λ₯Ό) λ³Ό μ μμ΅λλ€.
μ΅μ μ 보:
μ΄κ²μ λλ₯Ό μν΄ μΌνμ΅λλ€.
λλ ν΄λκ° posts
λ° pages
ν΄λ λ΄μ content
λ΄ gatsby-config.js
ν¬μΈνΈ content
.
export const query = graphql`
query IndexQuery {
allMarkdownRemark(
filter: { fileAbsolutePath: {regex : "\/posts/"} },
sort: {fields: [frontmatter___date], order: DESC},
) {
totalCount
edges {
node {
id
frontmatter {
title
slug
date(formatString: "DD MMMM YYYY")
category
}
excerpt
}
}
}
}
`;
@lukejanicke λ§μ§λ§ μ
λ°μ΄νΈ μ΄νλ‘ λ
Έλ μμ€ν
μ μ‘°κΈ λ λ°μ΄ λ€μμ΅λλ€. allFile
λ
Έλλ λ§ν¬ λ€μ΄ νΈλ€λ¬μ μν΄ κ΅¬λ¬Έ λΆμλκ³ μ allMarkdownRemark
λ
Έλκ° μμ±λ©λλ€. λ°λΌμ allFile
μμλ λ§ν¬ λ€μ΄ νμκ° μμλ νμ λ§ ββμμ±λκΈ° λλ¬Έμ onCreateNode
νν¬λ₯Ό ν΅ν΄ νμΌ λ
Έλμμ λ§ν¬ λ€μ΄ λ
Έλλ‘ μ»¬λ μ
μ΄λ¦ (νμΌ μμ€ν
μμ€ νλ¬κ·ΈμΈ ꡬμ±μ μ§μ λ¨)μ 볡μ¬νλ κ²μ΄ μμ΅λλ€. μμ λ₯Ό κ²μ ν μ½λλ μμ§λ§ λ§€μ° λΉ λ₯΄λ©° allMarkdownRemark
μμ νν°λ§ ν μμλ νλκ° μμ΅λλ€.
@KyleAMathewsκ° μ‘°μΈ ν λ λ€λ₯Έ κ°λ₯μ± (μ κ²½μ° μμ€ μ΄λ¦μ projects
)
allFile(filter: {internal: {mediaType: {eq: "text/markdown"}}, sourceInstanceName: {eq: "projects"}}) {
edges {
node {
childMarkdownRemark {
frontmatter {
...
}
}
}
}
}
@gbouteiller μ¬μ€, frontmatter
λ₯Ό μ»κ³ sourceInstanceName
λ‘ νν°λ§ ν μ μμ΅λλ€. λ¬Έμ λμ΄ μ κ·Ό λ°©μμ μ¬μ©νμ¬ frontmatter
λ΄λΆμ μ΄λ€ κ²λ μ λ ¬ ν μ μλ€λ frontmatter
μμ μ μ₯λ©λλ€.
μμ μΌμ κΈ°μ€μΌλ‘ μ λ ¬ ν μμλ λ°©λ²μ΄ μμ΅λλ€ frontmatter
μ μν΄ νν° sourceInstanceName
κΈ°λ³Έμ μΌλ‘λ. sourceInstanceName
μ File
λ
Έλμμ markdownRemark
λ
Έλλ‘ λ³΅μ¬λμ§ μμ΅λλ€. μ μΌν μ΅μ
μ μ κ·μμ μ¬μ©νκ±°λ μ΄μ κ²μλ¬Όμ λ°λΌ μΌλΆ λ°μ΄ν°λ₯Ό 볡μ¬νλ κ²μ
λλ€.
μ λ ¬μ΄ νμνμ§ μμ κ²½μ° (λ§μ κ²½μ°μ ν΄λΉ ν μ μμ) μ κ·Ό λ°©μμ΄ λ§€μ° ν©λ¦¬μ μ
λλ€. λλ λͺ°λλ€ childMarkdownRemark
μ νλλ‘ μ¬μ©ν μμμμ΅λλ€ file
, μκ³ λ§€μ° μ μ© κ³΅μ λ₯Όμν κ°μ¬μ΄.
"name"νλκ° markdownRemarkμ μ μλνμ§ μλλ€λ μ¬μ€μ μ€λ§νμ΅λλ€. μ΄ μ 보λ₯Ό μ λ¬νκΈ° μν΄ ν μμλ μμ μΉλ¦¬κ° μμ΅λκΉ?
νΈμ§ : μ‘°κΈ μλ§μΌλ‘ λ§λ ν id νλκ° κ²½λ‘λ₯Ό λ ΈμΆνκ³ μ΄λ₯Ό κΈ°λ°μΌλ‘ νν°λ§ ν μ μμμ κΉ¨λ¬μμ΅λλ€.
query AllQuery {
DSes: allMarkdownRemark(
limit: 3
filter: { id: { regex: "/_design-systems/" } }
) {
edges {
node {
frontmatter {
title
date
company
link
image
description
}
fields {
slug
}
}
}
}
Articles: allMarkdownRemark(
limit: 3
filter: { id: { regex: "/_articles/" } }
) {
edges {
node {
frontmatter {
title
date
company
link
image
description
}
fields {
slug
}
}
}
}
}
μμ€ νμΌ μμ€ν
μ name
νλλ μ¬μ ν gatsby-transformer-remarkμ λν΄ μ무 μμ
λ μννμ§ μμ΅λλ€. gatsby-transformer-remark
κ²°μ½ μ΄ν 쿼리 κ°λ₯ / νν°λ§ κ°λ₯ν νλλ‘μ΄λ₯Ό ν΅κ³Όνλλ‘ dxλ₯Ό μ½κ° κ°μ νλ€κ³ λ§ν μ μμ΅λλ€. gatsby-source-filesystem
μμ΄ μ‘΄μ¬ν©λλ€.
@ tsiq-swyx File
μμ MarkdownRemark
κΉμ§ File
collection
νλλ₯Ό μ λ¬νκΈ° μν΄ μ΄μ κ°μ κ²μ μ¬μ©νκ³ μμ΅λλ€.
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators
if (_.get(node, 'internal.type') === `MarkdownRemark`) {
// Get the parent node
const parent = getNode(_.get(node, 'parent'))
// Create a field on this node for the "collection" of the parent
// NOTE: This is necessary so we can filter `allMarkdownRemark` by
// `collection` otherwise there is no way to filter for only markdown
// documents of type `post`.
createNodeField({
node,
name: 'collection',
value: _.get(parent, 'sourceInstanceName'),
})
regex
μ κ·Ό λ°©μμ μλνμ§λ§ μ·¨μ½ν©λλ€.
@chmac μ΄κ²μ λ§€μ° μ리νκ³ μλν©λλ€. μ루μ
μ 곡μ ν΄ μ£Όμ
μ κ°μ¬ν©λλ€. @KyleAMathews νκ·Έλ₯Ό μ§μ ν΄μ λ―Έμνμ§λ§ (νκ·Έ ν λ€λ₯Έ μ¬λμ΄μλ κ²½μ° lmk) κΈ°λ³Έμ μΌλ‘ gatsby-source-filesystem
μ΄ μ΄λ¬ν λ°©μμΌλ‘ μλ ν μ μμ΅λκΉ (sourceInstanceNameμ λ
Έλ νλ λλ λ€λ₯Έ νλμ μ λ¬)?
@ tsiq-swyx 100 % νμ€νμ§λ μμ§λ§ μλΉν κ°λ¨ν ν 리νμ€νΈλΌκ³ μκ°ν©λλ€! ;-)
λλ κ°μΈ λΉ κ΄λ¦¬μκ° μ΄λ¬ν κ²λ€μ λν΄ κ°ν μ견μ κ°μ§κ³ μλ€κ³ μκ°νλ―λ‘ μμμ μμΉ μλ PRμ 보λ΄κΈ° μ μ λ Όμ ν κ°μΉκ° μλ€κ³ μκ°ν©λλ€. :)
μ’μ, μλνμ§λ§ λ΄ createPages λ΄λ³΄λ΄κΈ°λ νλμ ν νλ¦Ώ λ§ μ¬μ©ν©λλ€.
gatsby-source-filesystem
κ²μ΄ graphqlμ AllFileμ μ ν νμλ‘νμ§ μλλ€λ κ²μ ν₯λ―Έ λ‘μ΅λλ€. νΌλμ κ°μ Έμ¬ μ μμΌλ©° λ¬Έμμ λ°μλμ§ μμ΅λλ€.
λλ μ¬μ©νκ³ μλ€
const { createPage } = actions
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
frontmatter {
lang
}
}
}
}
}
`)
λ¬Έμ μμ΄ νμ΄μ§λ₯Ό μμ±ν©λλ€. κ·Έλ¬λ gatsby-source-git
μλ λμΌν μμΉμ΄ μ μ©λλμ§ νμ€νμ§ μμ΅λλ€.
κ°μ¬ν©λλ€ @chmac
gatsby-source-filesystem
μ΄λ¦μ κΈ°μ€μΌλ‘ νμ΄μ§λ₯Ό λ§λ€μ΄μΌν©λλ€ ...
κ·Έλμ μ¬κΈ° λ΄ ν΄κ²°μ± μ΄ μμ΅λλ€.
const { createFilePath } = require("gatsby-source-filesystem")
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx` && getNode(node.parent).sourceInstanceName === "careers") {
const value = createFilePath({ node, getNode })
createNodeField({
name: "slug",
node,
value: `/careers${value}`
})
}
}
μ΄κ²μ intresting, κ·Έλ¬ν μ΅μ μ μ°Ύμ§ λͺ»νμ΅λλ€.
2020 λ 4 μ 27 μΌ μμμΌ, Noman Gul [email protected] μ λ€μκ³Ό κ°μ΄ μΌμ΅λλ€.
κ°μ¬ν©λλ€ @chmac https://github.com/chmac
gatsby-source-filesystem μ΄λ¦μ κΈ°λ°μΌλ‘ νμ΄μ§λ₯Ό λ§λ€μ΄μΌν©λλ€ ...
κ·Έλμ μ¬κΈ° λ΄ ν΄κ²°μ± μ΄ μμ΅λλ€.
const {createFilePath} = require ( "gatsby-source-filesystem")
exports.onCreateNode = ({node, actions, getNode}) => {
const {createNodeField} = μμif (node.internal.type ===
Mdx
&& getNode (node.parent) .sourceInstanceName === "careers") {
const κ° = createFilePath ({node, getNode})createNodeField({ name: "slug", node, value: `/careers${value}` })
}
}β
λκΈμ λ¬μ κΈ° λλ¬Έμ μμ ν κ²μ λλ€.
μ΄ μ΄λ©μΌμ μ§μ λ΅μ₯νκ³ GitHubμμ νμΈνμΈμ.
https://github.com/gatsbyjs/gatsby/issues/1634#issuecomment-619670883 ,
λλ ꡬλ μ·¨μ
https://github.com/notifications/unsubscribe-auth/AAEQPFX6KR7NUIY33Q7V4MDROTS7PANCNFSM4DUUPK6A
.
λλμ΄ κΈμ μ½μ ν κ½€ νν κ²½μ°λΌκ³ μκ°νλ κ²κ³Ό κ°μ μν©μμλ€. μ μκ°μ κ°μ₯ μ’κ³ ν΄νΉλμ§ μμ μ루μ μ λ€μκ³Ό κ°μ΅λλ€.
{
allMarkdownRemark(filter: {fileAbsolutePath: {regex: "/projects/"}}) {
edges {
node {
frontmatter {
title
}
}
}
}
}
λ΄ projects
μ½ν
μΈ μ νμ /content/projects/
μμ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
@ tsiq-swyx
File
μμMarkdownRemark
κΉμ§File
collection
νλλ₯Ό μ λ¬νκΈ° μν΄ μ΄μ κ°μ κ²μ μ¬μ©νκ³ μμ΅λλ€.regex
μ κ·Ό λ°©μμ μλνμ§λ§ μ·¨μ½ν©λλ€.