Skip to content

Commit

Permalink
Support markdoc-it's typographer option in markdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
schpet committed Jul 10, 2024
1 parent 88e2b43 commit d625c17
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/forty-scissors-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdoc': minor
---

Add support for markdoc-it's typographer option
1 change: 1 addition & 0 deletions packages/integrations/markdoc/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface MarkdocIntegrationOptions {
allowHTML?: boolean;
ignoreIndentation?: boolean;
typographer?: boolean;
}
5 changes: 5 additions & 0 deletions packages/integrations/markdoc/src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export function getMarkdocTokenizer(options: MarkdocIntegrationOptions | undefin
// allow indentation so nested Markdoc tags can be formatted for better readability
tokenizerOptions.allowIndentation = true;
}
if (options?.typographer) {
// enable typographer to convert straight quotes to curly quotes, etc.
tokenizerOptions.typographer = options.typographer;
}


_cachedMarkdocTokenizers[key] = new Markdoc.Tokenizer(tokenizerOptions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import markdoc from '@astrojs/markdoc';
import { defineConfig } from 'astro/config';

// https://astro.build/config
export default defineConfig({
integrations: [markdoc({ typographer: true })],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/markdoc-render-typographer",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/markdoc": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Typographer
---

## Typographer's post

This is a post to test the "typographer" option.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
import { getEntryBySlug } from "astro:content";
const post = await getEntryBySlug('blog', 'typographer');
const { Content } = await post.render();
---

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Content</title>
</head>
<body>
<Content />
</body>
</html>
22 changes: 22 additions & 0 deletions packages/integrations/markdoc/test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ describe('Markdoc - render', () => {

renderWithRootFolderContainingSpace(html);
});

it('renders content - with typographer option', async () => {
const fixture = await getFixture('render-typographer');
await fixture.build()

const html = await fixture.readFile('/index.html');

renderTypographerChecks(html);
});
});
});

Expand Down Expand Up @@ -173,3 +182,16 @@ function renderWithRootFolderContainingSpace(html) {
const p = document.querySelector('p');
assert.equal(p.textContent, 'This is a simple Markdoc post with root folder containing a space.');
}

/**
* @param {string} html
*/
function renderTypographerChecks(html) {
const { document } = parseHTML(html);

const h2 = document.querySelector('h2');
assert.equal(h2.textContent, 'Typographer’s post');

const p = document.querySelector('p');
assert.equal(p.textContent, 'This is a post to test the “typographer” option.');
}
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d625c17

Please sign in to comment.