Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Initial working version with MDX remote #9

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"semi": false
}
8 changes: 4 additions & 4 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
// [[wiki-links]], backlinking etc
"kortina.vscode-markdown-notes",

// Adds `show graph` command that displays graph of linked notes
"tchayen.markdown-links",
// Image-pasting for markdown
"mushan.vscode-paste-image",

// Understated grayscale theme (light and dark variants)
"philipbe.theme-gray-matter"
// Spell checking for text, markdown and latex
"ban.spellright",
]
}
20 changes: 15 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
"editor.minimap.enabled": false,
"editor.wrappingIndent": "indent",
"editor.overviewRulerBorder": false,
"editor.lineHeight": 24,
"workbench.colorTheme": "Gray Matter Light",
"[markdown]": {
"editor.lineHeight": 24,
"editor.quickSuggestions": true
},
"git.enableSmartCommit": true,
"git.postCommitCommand": "sync",
"files.defaultLanguage": "markdown",
"files.exclude": {
"**/node_modules": true
},
"files.watcherExclude": {
"**/node_modules": true
},
"foam.edit.linkReferenceDefinitions": "withExtensions",
"foam.openDailyNote.directory": "_posts",
"foam.openDailyNote.fileExtension": "mdx",
"foam.openDailyNote.titleFormat": "fullDate",
"git.enableSmartCommit": true,
"git.postCommitCommand": "sync",
"markdown.preview.breaks": true,
"pasteImage.path": "${projectRoot}/public/images",
"pasteImage.showFilePathConfirmInputBox": true,
"spellright.notificationClass": "warning",
"vscodeMarkdownNotes.defaultFileExtension": "mdx",
"vscodeMarkdownNotes.noteCompletionConvention": "noExtension",
"vscodeMarkdownNotes.slugifyMethod": "github-slugger"
"vscodeMarkdownNotes.slugifyMethod": "github-slugger",
"window.autoDetectColorScheme": true
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import Button from '../components/button.js'

# JSX Component in Mdx Example

Look, a button! 👇

<Button>👋 Hello</Button>


File renamed without changes.
8 changes: 0 additions & 8 deletions pages/readme.md → _posts/readme.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,3 @@ We've created a few Bubbles (markdown documents) to get you started.
They should work as expected in VS Code, and in rendered GitHub Pages.

If GitHub preview (or general 100% support with all Markdown tools) is a requirement, for the time being you can use the standard `[description](page.md)` syntax.



[//begin]: # "Autogenerated link references for markdown compatibility"
[inbox]: inbox "Inbox"
[foam-tips]: foam-tips "Foam tips"
[todo]: todo "Todo"
[//end]: # "Autogenerated link references"
File renamed without changes.
19 changes: 0 additions & 19 deletions next.config.js

This file was deleted.

101 changes: 0 additions & 101 deletions package-lock.json

This file was deleted.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
"start": "next start"
},
"dependencies": {
"@mdx-js/loader": "^1.6.21",
"@next/mdx": "^10.0.1",
"gray-matter": "4.0.2",
"next": "^10.0.1",
"next-mdx-remote": "2.1.3",
"prism-react-renderer": "^1.1.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"remark-external-links": "^7.0.0",
"remark-images": "^2.0.0",
"remark-inline-links": "5.0.0",
"remark-numbered-footnote-labels": "^1.1.0",
"remark-unwrap-images": "^2.0.0"
"remark-unwrap-images": "^2.0.0",
"remark-wiki-link": "0.0.4"
},
"devDependencies": {},
"license": "ISC"
Expand Down
84 changes: 84 additions & 0 deletions pages/[...slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import fs from 'fs'
import matter from 'gray-matter'
import hydrate from 'next-mdx-remote/hydrate'
import renderToString from 'next-mdx-remote/render-to-string'
import dynamic from 'next/dynamic'
import path from 'path'
import remarkExternalLinks from 'remark-external-links'
import remarkImages from 'remark-images'
import remarkFootnote from 'remark-numbered-footnote-labels'
import remarkUnwrapImages from 'remark-unwrap-images'
import remarkWikiLink from 'remark-wiki-link'
import remarkInlineLinks from 'remark-inline-links'
import AnchorTag from '../components/AnchorTag'
import CodeBlock from '../components/CodeBlock'
import Image from '../components/Image'
import { postFilePaths, POSTS_PATH } from '../utils/mdxUtils'

// prettier-ignore
const components = {
a: props => <AnchorTag {...props} />,
code: CodeBlock,
img: (props) => <div className="nextImageWrapper"><Image {...props} /></div>,
Image: (props) => <div className="nextImageWrapper"><Image {...props} /></div>,
Button: dynamic(() => import('../components/button')),
}

export default function PostPage({ source, frontMatter }) {
const content = hydrate(source, { components })
return content
}

export const getStaticProps = async ({ params }) => {
let postPath = 'index'

if (params && params.slug) {
postPath = Array.isArray(params.slug) ? params.slug.join('/') : params.slug
}

const postFilePath = path.join(POSTS_PATH, `${postPath}.mdx`)
const source = fs.readFileSync(postFilePath)

const { content, data } = matter(source)

const mdxSource = await renderToString(content, {
components,
mdxOptions: {
remarkPlugins: [
remarkImages,
remarkExternalLinks,
remarkFootnote,
remarkInlineLinks,
remarkUnwrapImages,
[
remarkWikiLink,
{
hrefTemplate: (permalink) => `${permalink}`,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like here is where remarkWikiLink is not returning the full path. Line 56 outputs /note instead of /note/note. A possible fix is to construct the correct href from postFilePath.

pageResolver: (name) => [name.replace(/ /g, '-').toLowerCase()],
Copy link
Owner

@yenly yenly Feb 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart idea to use remark-wiki-link to remove the [] but kebab case links aren't the desired look. My AnchorTag component converts it to use the title of the note. Here you are converting the link back to the kebab case. Capitalize case like how Foam documentation displays is best.

},
],
],
},
scope: data,
})

return {
props: {
source: mdxSource,
frontMatter: data,
},
}
}

export const getStaticPaths = async () => {
const paths = postFilePaths
// Remove file extensions for page paths
.map((path) => path.replace(/\.mdx?$/, ''))
// Map the path into the static paths object required by Next.js
.map((slug) => ({ params: { slug: slug.split('/') } }))

return {
paths,
fallback: false,
}
}
16 changes: 3 additions & 13 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@ import '../styles/global.css'
import '../styles/prism-theme-night-owl.css'
import Layout from '../components/Layout'
import siteData from '../siteconfig'
import { MDXProvider } from '@mdx-js/react'
import CodeBlock from '../components/CodeBlock'
import AnchorTag from '../components/AnchorTag'
import Image from '../components/Image'

const mdComponents = {
a: props => <AnchorTag {...props} />,
code: CodeBlock,
img: (props) => <div className="nextImageWrapper"><Image {...props} /></div>,
Image: (props) => <div className="nextImageWrapper"><Image {...props} /></div>
}

export default function App({ Component, pageProps }) {
return (
<MDXProvider components={mdComponents}>
<Layout siteData={siteData}>
<Component {...pageProps} />
</Layout>
</MDXProvider>
<Layout siteData={siteData}>
<Component {...pageProps} />
</Layout>
)
}
1 change: 1 addition & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, getStaticProps } from './[...slug]'
28 changes: 28 additions & 0 deletions utils/mdxUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from 'fs'
import path from 'path'

// POSTS_PATH is useful when you want to get the path to a specific file
export const POSTS_PATH = path.join(process.cwd(), '_posts')

const getAllFiles = function (dirPath, arrayOfFiles) {
let files = fs.readdirSync(dirPath)

arrayOfFiles = arrayOfFiles || []

files.forEach(function (file) {
if (fs.statSync(dirPath + '/' + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + '/' + file, arrayOfFiles)
} else {
arrayOfFiles.push(
path.join(dirPath, '/', file).replace(POSTS_PATH + '/', '')
)
}
})

return arrayOfFiles
}

// postFilePaths is the list of all mdx files inside the POSTS_PATH directory
export const postFilePaths = getAllFiles(POSTS_PATH)
// Only include md(x) files
.filter((path) => /\.mdx?$/.test(path))
Loading