Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(markdoc): Support markdown-it's typographer option #11450

Merged
merged 6 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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': patch
---

Adds support for markdown-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.');
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

Loading