Giter Club home page Giter Club logo

mordred's Introduction

๐Ÿคบ Mordred

npm version community

Source data from anywhere, for Next.js, Nuxt.js, Eleventy and many more.

Features

โœ… Query any data (Markdown, API, database, CMS) with GraphQL
โœ… Framework agnostic, works with any framework that has SSG support
โœ… Tons of plugins for popular headless CMS (not yet, we need your contribution!)

Table of Contents

Install

yarn add mordred

Usage with Next.js

Configuration

In next.config.js:

module.exports = {
  webpack(config) {
    const { MordredWebpackPlugin } = require('mordred/webpack')

    const mordredPlugin = new MordredWebpackPlugin()
    config.plugins.push(mordredPlugin)
    return config
  },
}

Then create a mordred.config.js in the same directory and use some plugins:

module.exports = {
  plugins: [
    {
      resolve: 'mordred-source-filesystem',
      options: {
        // This is where you'll be creating Markdown files
        path: __dirname + '/content',
      },
    },
    {
      resolve: 'mordred-transformer-markdown',
    },
  ],
}

You also need to install these plugins:

yarn add mordred-source-filesystem mordred-transformer-markdown

Using Data

Create a Markdown file in content folder (in your project root), like content/my-first-posts.md:

---
title: My First Post
date: 2020-04-24
---

This is my **first** post!

When you run next or next build, Mordred will generate a GraphQL client at mordred/graphql.js, then you can use the generated client to query data.

Now in any page, query data in getStaticProps:

import { query, gql } from '../mordred/graphql'

export const getStaticProps = async () => {
  const { data, errors } = await query(gql`
    {
      allMarkdown {
        nodes {
          id
          slug
          createdAt
          updatedAt
          html
          frontmatter {
            # ... or any frontmatter
            # like:
            title
          }
        }
      }
    }
  `)
  if (errors) {
    throw errors[0]
  }
  return {
    props: {
      ...data,
    },
  }
}

export default ({ allMarkdown }) => {
  return (
    <ul>
      {allMarkdown.nodes.map((post) => {
        return (
          <li key={post.id}>
            <Link href={`/post/${post.slug}`}>{post.title}</Link>
          </li>
        )
      })}
    </ul>
  )
}

Exploring Data with GraphiQL

You can create an API at /api/graphql to explore data via GraphiQL:

import express from 'express'
import graphqlHTTP from 'express-graphql'
import { schema } from '../../mordred/graphql'

const app = express()

app.use(
  graphqlHTTP({
    schema,
    graphiql: true,
  })
)

export default app

Usage With Nuxt.js

We're waiting for Nuxt's full-static mode, it's already possible to use Mordred with Nuxt's asyncData though. We'll document this soon.

Plugin List

License

MIT ยฉ EGOIST (Kevin Titor)

mordred's People

Contributors

egoist avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.