0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

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

* Support markdoc-it's typographer option in markdoc

* Update .changeset/forty-scissors-jog.md [skip ci]

* Update .changeset/forty-scissors-jog.md [skip ci]

* Fix typo in changeset

---------

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
This commit is contained in:
Peter Schilling 2024-07-17 05:14:35 -07:00 committed by GitHub
parent 3b94324228
commit eb303e1ad5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 84 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/markdoc': patch
---
Adds support for markdown-it's typographer option

View file

@ -1,4 +1,5 @@
export interface MarkdocIntegrationOptions {
allowHTML?: boolean;
ignoreIndentation?: boolean;
typographer?: boolean;
}

View file

@ -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);
}

View file

@ -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 })],
});

View file

@ -0,0 +1,9 @@
{
"name": "@test/markdoc-render-typographer",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/markdoc": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,7 @@
---
title: Typographer
---
## Typographer's post
This is a post to test the "typographer" option.

View file

@ -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>

View file

@ -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);
});
});
});
@ -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, 'Typographers post');
const p = document.querySelector('p');
assert.equal(p.textContent, 'This is a post to test the “typographer” option.');
}

View file

@ -4597,6 +4597,15 @@ importers:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/markdoc/test/fixtures/render-typographer:
dependencies:
'@astrojs/markdoc':
specifier: workspace:*
version: link:../../..
astro:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/markdoc/test/fixtures/render-with-components:
dependencies:
'@astrojs/markdoc':