Skip to content

Commit d384d1a

Browse files
authored
Feat/rehype-citation (#270)
* feat: add rehype-citation and upgrade to next 12.0.3 * docs: update docs
1 parent 2239743 commit d384d1a

File tree

8 files changed

+426
-167
lines changed

8 files changed

+426
-167
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Feature request? Check the past discussions to see if it has been brough up prev
2626
- [David Levai's digital garden](https://davidlevai.com/) - customized design and added email subscriptions
2727
- [Thinh's Corner](https://thinhcorner.com/) - [customized layout](https://github.com/Th1nhNg0/th1nhng0.vercel.app/blob/5e73a420828d82f01e7147512a2c3273c4ec19f8/layouts/PostLayout.js) with sticky side table of contents
2828
- [Leo's Blog](https://leohuynh.dev) - Tuan Anh Huynh's personal site. Add Snippets Page, Author Profile Card, Image Lightbox, ...
29-
- [thvu.dev](https://thvu.dev) - Added `mdx-embed`, view count, reading minutes and more. Keep reading at: [Over-engineering thvu.dev](https://www.thvu.dev/blog/over-engineering-thvu-dev)
29+
- [thvu.dev](https://thvu.dev) - Added `mdx-embed`, view count, reading minutes and more.
3030

3131
Using the template? Feel free to create a PR and add your blog to this list.
3232

@@ -47,6 +47,7 @@ I wanted it to be nearly as feature-rich as popular blogging templates like [bea
4747
- [MDX - write JSX in markdown documents!](https://mdxjs.com/)
4848
- Server-side syntax highlighting with line numbers and line highlighting via [rehype-prism-plus](https://github.com/timlrx/rehype-prism-plus)
4949
- Math display supported via [KaTeX](https://katex.org/)
50+
- Citation and biliography support via [rehype-citation](https://github.com/timlrx/rehype-citation)
5051
- Automatic image optimization via [next/image](https://nextjs.org/docs/basic-features/image-optimization)
5152
- Flexible data retrieval with [mdx-bundler](https://github.com/kentcdodds/mdx-bundler)
5253
- Support for tags - each unique tag will be its own page

css/prism.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929

3030
.line-number::before {
31-
@apply mr-4 -ml-2 text-gray-400 inline-block w-4 text-right;
31+
@apply inline-block w-4 mr-4 -ml-2 text-right text-gray-400;
3232
content: attr(line);
3333
}
3434

data/blog/introducing-tailwind-nextjs-starter-blog.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: 'Introducing Tailwind Nextjs Starter Blog'
33
date: '2021-01-12'
4-
lastmod: '2021-09-08'
4+
lastmod: '2021-11-14'
55
tags: ['next-js', 'tailwind', 'guide']
66
draft: false
77
summary: 'Looking for a performant, out of the box template, with all the best in web technology to support your blogging needs? Checkout the Tailwind Nextjs Starter Blog template.'
@@ -51,6 +51,7 @@ I wanted it to be nearly as feature-rich as popular blogging templates like [bea
5151
- [MDX - write JSX in markdown documents!](https://mdxjs.com/)
5252
- Server-side syntax highlighting with line numbers and line highlighting via [rehype-prism-plus](https://github.com/timlrx/rehype-prism-plus)
5353
- Math display supported via [KaTeX](https://katex.org/)
54+
- Citation and biliography support via [rehype-citation](https://github.com/timlrx/rehype-citation)
5455
- Automatic image optimization via [next/image](https://nextjs.org/docs/basic-features/image-optimization)
5556
- Flexible data retrieval with [mdx-bundler](https://github.com/kentcdodds/mdx-bundler)
5657
- Support for tags - each unique tag will be its own page

data/blog/new-features-in-v1.mdx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
22
title: 'New features in v1'
33
date: 2021-08-07T15:32:14Z
4-
lastmod: '2021-10-27'
4+
lastmod: '2021-11-14'
55
tags: ['next-js', 'tailwind', 'guide']
66
draft: false
77
summary: 'An overview of the new features released in v1 - code block copy, multiple authors, frontmatter layout and more'
88
layout: PostSimple
9+
bibliography: references-data.bib
910
---
1011

1112
## Overview
@@ -329,7 +330,7 @@ To modify the styles, change the following class selectors in the `tailwind.css`
329330
}
330331
```
331332

332-
## Newletter component
333+
## Newletter component (v1.1.3)
333334

334335
Introduced in v1.1.3, the newsletter component gives you a easy way to build an audience. It integrates with the following providers:
335336

@@ -354,6 +355,37 @@ and is not compatible with a 100% static site export. Users should either self-h
354355

355356
A static site compatible alternative is to substitute the route in the newsletter component with a form API endpoint provider.
356357

358+
## Bibliography and Citations (v1.2.1)
359+
360+
`rehype-citation` plugin is added to the xdm processing pipeline in v1.2.1. This allows you to easily format citations and insert bibliography from an existing bibtex or CSL-json file.
361+
362+
For example, the following markdown code sample:
363+
364+
```md
365+
Standard citation [@Nash1950]
366+
In-text citations e.g. @Nash1951
367+
Multiple citations [see @Nash1950; @Nash1951, page 50]
368+
369+
**References:**
370+
371+
[^ref]
372+
```
373+
374+
is rendered to the following:
375+
376+
Standard citation [@Nash1950]
377+
In-text citations e.g. @Nash1951
378+
Multiple citations [see @Nash1950; @Nash1951, page 50]
379+
380+
**References:**
381+
382+
[^ref]
383+
384+
A bibliography will be inserted at the end of the document, but this can be overwritten by specifying a `[^Ref]` tag at the intended location.
385+
The plugin uses APA citation formation, but also supports the following CSLs, 'apa', 'vancouver', 'harvard1', 'chicago', 'mla', or a path to a user-specified CSL file.
386+
387+
See [rehype-citation readme](https://github.com/timlrx/rehype-citation) for more information on the configuration options.
388+
357389
## Upgrade guide
358390

359391
There are significant portions of the code that has been changed from v0 to v1 including support for layouts and a new mdx engine.

data/references-data.bib

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@article{Nash1950,
2+
title={Equilibrium points in n-person games},
3+
author={Nash, John},
4+
journal={Proceedings of the national academy of sciences},
5+
volume={36},
6+
number={1},
7+
pages={48--49},
8+
year={1950},
9+
publisher={USA}
10+
}
11+
12+
@article{Nash1951,
13+
title={Non-cooperative games},
14+
author={Nash, John},
15+
journal={Annals of mathematics},
16+
pages={286--295},
17+
year={1951},
18+
publisher={JSTOR}
19+
}
20+
21+
@Manual{Macfarlane2006,
22+
url={https://pandoc.org/},
23+
title={Pandoc: a universal document converter},
24+
author={MacFarlane, John},
25+
year={2006}
26+
}
27+
28+
@book{Xie2016,
29+
title={Bookdown: authoring books and technical documents with R markdown},
30+
author={Xie, Yihui},
31+
year={2016},
32+
publisher={CRC Press}
33+
}

lib/mdx.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import remarkImgToJsx from './remark-img-to-jsx'
1616
import rehypeSlug from 'rehype-slug'
1717
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
1818
import rehypeKatex from 'rehype-katex'
19+
import rehypeCitation from 'rehype-citation'
1920
import rehypePrismPlus from 'rehype-prism-plus'
2021

2122
const root = process.cwd()
@@ -46,27 +47,18 @@ export async function getFileBySlug(type, slug) {
4647

4748
// https://github.com/kentcdodds/mdx-bundler#nextjs-esbuild-enoent
4849
if (process.platform === 'win32') {
49-
process.env.ESBUILD_BINARY_PATH = path.join(
50-
process.cwd(),
51-
'node_modules',
52-
'esbuild',
53-
'esbuild.exe'
54-
)
50+
process.env.ESBUILD_BINARY_PATH = path.join(root, 'node_modules', 'esbuild', 'esbuild.exe')
5551
} else {
56-
process.env.ESBUILD_BINARY_PATH = path.join(
57-
process.cwd(),
58-
'node_modules',
59-
'esbuild',
60-
'bin',
61-
'esbuild'
62-
)
52+
process.env.ESBUILD_BINARY_PATH = path.join(root, 'node_modules', 'esbuild', 'bin', 'esbuild')
6353
}
6454

6555
let toc = []
6656

67-
const { frontmatter, code } = await bundleMDX(source, {
57+
// Parsing frontmatter here to pass it in as options to rehype plugin
58+
const { data: frontmatter } = matter(source)
59+
const { code } = await bundleMDX(source, {
6860
// mdx imports can be automatically source from the components directory
69-
cwd: path.join(process.cwd(), 'components'),
61+
cwd: path.join(root, 'components'),
7062
xdmOptions(options) {
7163
// this is the recommended way to add custom remark/rehype plugins:
7264
// The syntax might look weird, but it protects you in case we add/remove
@@ -85,6 +77,10 @@ export async function getFileBySlug(type, slug) {
8577
rehypeSlug,
8678
rehypeAutolinkHeadings,
8779
rehypeKatex,
80+
[
81+
rehypeCitation,
82+
{ bibliography: frontmatter?.bibliography, path: path.join(root, 'data') },
83+
],
8884
[rehypePrismPlus, { ignoreMissing: true }],
8985
]
9086
return options

0 commit comments

Comments
 (0)