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

Add "allowHTML" option for Markdoc with HTML parsing/processing #7597

Merged
merged 17 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 2 additions & 0 deletions packages/integrations/markdoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"esbuild": "^0.17.19",
"github-slugger": "^2.0.0",
"gray-matter": "^4.0.3",
"htmlparser2": "^9.0.0",
"kleur": "^4.1.5",
"shiki": "^0.14.1",
"zod": "^3.17.3"
Expand All @@ -80,6 +81,7 @@
"@astrojs/markdown-remark": "^2.2.1",
"@types/chai": "^4.3.5",
"@types/html-escaper": "^3.0.0",
"@types/markdown-it": "^12.2.3",
"@types/mocha": "^9.1.1",
"astro": "workspace:*",
"astro-scripts": "workspace:*",
Expand Down
27 changes: 18 additions & 9 deletions packages/integrations/markdoc/src/content-entry-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,33 @@ import path from 'node:path';
import type * as rollup from 'rollup';
import type { MarkdocConfigResult } from './load-config.js';
import { setupConfig } from './runtime.js';
import { getMarkdocTokenizer } from './tokenizer.js';
import type { MarkdocIntegrationOptions } from './options.js';
import { htmlTokenTransform } from './html/transform/html-token-transform.js';

export async function getContentEntryType({
markdocConfigResult,
astroConfig,
options,
}: {
astroConfig: AstroConfig;
markdocConfigResult?: MarkdocConfigResult;
options?: MarkdocIntegrationOptions,

}): Promise<ContentEntryType> {
return {
extensions: ['.mdoc'],
getEntryInfo,
handlePropagation: true,
async getRenderModule({ contents, fileUrl, viteId }) {
const entry = getEntryInfo({ contents, fileUrl });
const tokens = markdocTokenizer.tokenize(entry.body);
const tokenizer = getMarkdocTokenizer(options);
let tokens = tokenizer.tokenize(entry.body);

if (options?.allowHTML) {
tokens = htmlTokenTransform(tokenizer, tokens);
}

const ast = Markdoc.parse(tokens);
const usedTags = getUsedTags(ast);
const userMarkdocConfig = markdocConfigResult?.config ?? {};
Expand All @@ -51,7 +63,7 @@ export async function getContentEntryType({
}

const pluginContext = this;
const markdocConfig = await setupConfig(userMarkdocConfig);
const markdocConfig = await setupConfig(userMarkdocConfig, options);

const filePath = fileURLToPath(fileUrl);

Expand Down Expand Up @@ -113,15 +125,18 @@ ${getStringifiedImports(componentConfigByNodeMap, 'Node', astroConfig.root)}
const tagComponentMap = ${getStringifiedMap(componentConfigByTagMap, 'Tag')};
const nodeComponentMap = ${getStringifiedMap(componentConfigByNodeMap, 'Node')};

const options = ${JSON.stringify(options)};

const stringifiedAst = ${JSON.stringify(
/* Double stringify to encode *as* stringified JSON */ JSON.stringify(ast)
)};

export const getHeadings = createGetHeadings(stringifiedAst, markdocConfig);
export const getHeadings = createGetHeadings(stringifiedAst, markdocConfig, options);
export const Content = createContentComponent(
Renderer,
stringifiedAst,
markdocConfig,
options,
tagComponentMap,
nodeComponentMap,
)`;
Expand All @@ -134,12 +149,6 @@ export const Content = createContentComponent(
};
}

const markdocTokenizer = new Markdoc.Tokenizer({
// Strip <!-- comments --> from rendered output
// Without this, they're rendered as strings!
allowComments: true,
});

function getUsedTags(markdocAst: Node) {
const tags = new Set<string>();
const validationErrors = Markdoc.validate(markdocAst);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import { styleToObject } from "./style-to-object.js";

export function parseInlineCSSToReactLikeObject(css: string | undefined | null): React.CSSProperties | undefined {
if (typeof css === "string") {
const cssObject: Record<string, string> = {};
styleToObject(css, (originalCssDirective: string, value: string) => {
const reactCssDirective = convertCssDirectiveNameToReactCamelCase(originalCssDirective);
cssObject[reactCssDirective] = value;
});
return cssObject;
}

return undefined;
}

function convertCssDirectiveNameToReactCamelCase(original: string): string {
// capture group 1 is the character to capitalize, the hyphen is omitted by virtue of being outside the capture group
const replaced = original.replace(/-([a-z0-9])/ig, (_match, char) => {
return char.toUpperCase();
});
return replaced;
}
278 changes: 278 additions & 0 deletions packages/integrations/markdoc/src/html/css/parse-inline-styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
// @ts-nocheck
// https://github.com/remarkablemark/inline-style-parser

/**
* @license MIT
*
* (The MIT License)
*
* Copyright (c) 2012 TJ Holowaychuk <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// http://www.w3.org/TR/CSS21/grammar.html
// https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027
const COMMENT_REGEX = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;

const NEWLINE_REGEX = /\n/g;
const WHITESPACE_REGEX = /^\s*/;

// declaration
const PROPERTY_REGEX = /^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/;
const COLON_REGEX = /^:\s*/;
const VALUE_REGEX = /^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/;
const SEMICOLON_REGEX = /^[;\s]*/;

// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill
const TRIM_REGEX = /^\s+|\s+$/g;

// strings
const NEWLINE = '\n';
const FORWARD_SLASH = '/';
const ASTERISK = '*';
const EMPTY_STRING = '';

// types
const TYPE_COMMENT = 'comment';
const TYPE_DECLARATION = 'declaration';

/**
* @param {String} style
* @param {Object} [options]
* @return {Object[]}
* @throws {TypeError}
* @throws {Error}
*/
export function parseInlineStyles(style, options) {
if (typeof style !== 'string') {
throw new TypeError('First argument must be a string');
}

if (!style) return [];

options = options || {};

/**
* Positional.
*/
let lineno = 1;
let column = 1;

/**
* Update lineno and column based on `str`.
*
* @param {String} str
*/
function updatePosition(str) {
let lines = str.match(NEWLINE_REGEX);
if (lines) lineno += lines.length;
let i = str.lastIndexOf(NEWLINE);
column = ~i ? str.length - i : column + str.length;
}

/**
* Mark position and patch `node.position`.
*
* @return {Function}
*/
function position() {
let start = { line: lineno, column: column };
return function (node) {
node.position = new Position(start);
whitespace();
return node;
};
}

/**
* Store position information for a node.
*
* @constructor
* @property {Object} start
* @property {Object} end
* @property {undefined|String} source
*/
function Position(start) {
this.start = start;
this.end = { line: lineno, column: column };
this.source = options.source;
}

/**
* Non-enumerable source string.
*/
Position.prototype.content = style;

const errorsList = [];

/**
* Error `msg`.
*
* @param {String} msg
* @throws {Error}
*/
function error(msg) {
const err = new Error(
options.source + ':' + lineno + ':' + column + ': ' + msg
);
err.reason = msg;
err.filename = options.source;
err.line = lineno;
err.column = column;
err.source = style;

if (options.silent) {
errorsList.push(err);
} else {
throw err;
}
}

/**
* Match `re` and return captures.
*
* @param {RegExp} re
* @return {undefined|Array}
*/
function match(re) {
const m = re.exec(style);
if (!m) return;
const str = m[0];
updatePosition(str);
style = style.slice(str.length);
return m;
}

/**
* Parse whitespace.
*/
function whitespace() {
match(WHITESPACE_REGEX);
}

/**
* Parse comments.
*
* @param {Object[]} [rules]
* @return {Object[]}
*/
function comments(rules) {
let c;
rules = rules || [];
while ((c = comment())) {
if (c !== false) {
rules.push(c);
}
}
return rules;
}

/**
* Parse comment.
*
* @return {Object}
* @throws {Error}
*/
function comment() {
const pos = position();
if (FORWARD_SLASH != style.charAt(0) || ASTERISK != style.charAt(1)) return;

let i = 2;
while (
EMPTY_STRING != style.charAt(i) &&
(ASTERISK != style.charAt(i) || FORWARD_SLASH != style.charAt(i + 1))
) {
++i;
}
i += 2;

if (EMPTY_STRING === style.charAt(i - 1)) {
return error('End of comment missing');
}

const str = style.slice(2, i - 2);
column += 2;
updatePosition(str);
style = style.slice(i);
column += 2;

return pos({
type: TYPE_COMMENT,
comment: str
});
}

/**
* Parse declaration.
*
* @return {Object}
* @throws {Error}
*/
function declaration() {
const pos = position();

// prop
const prop = match(PROPERTY_REGEX);
if (!prop) return;
comment();

// :
if (!match(COLON_REGEX)) return error("property missing ':'");

// val
const val = match(VALUE_REGEX);

const ret = pos({
type: TYPE_DECLARATION,
property: trim(prop[0].replace(COMMENT_REGEX, EMPTY_STRING)),
value: val
? trim(val[0].replace(COMMENT_REGEX, EMPTY_STRING))
: EMPTY_STRING
});

// ;
match(SEMICOLON_REGEX);

return ret;
}

/**
* Parse declarations.
*
* @return {Object[]}
*/
function declarations() {
const decls = [];

comments(decls);

// declarations
let decl;
while ((decl = declaration())) {
if (decl !== false) {
decls.push(decl);
comments(decls);
}
}

return decls;
}

whitespace();
return declarations();
};

/**
* Trim `str`.
*
* @param {String} str
* @return {String}
*/
function trim(str) {
return str ? str.replace(TRIM_REGEX, EMPTY_STRING) : EMPTY_STRING;
}
Loading