summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/src/html/css/style-to-object.ts
diff options
context:
space:
mode:
authorGravatar Alex Sherwin <alex-sherwin@users.noreply.github.com> 2023-07-24 19:34:06 -0400
committerGravatar GitHub <noreply@github.com> 2023-07-24 19:34:06 -0400
commit7461e82c81438df956861197536f9ceeaf63d6b3 (patch)
tree5d5318b0f8fd61eba466cbccb00c8e5035914b57 /packages/integrations/markdoc/src/html/css/style-to-object.ts
parent81c460e30b3318b9727a609390243f42e112ad24 (diff)
downloadastro-7461e82c81438df956861197536f9ceeaf63d6b3.tar.gz
astro-7461e82c81438df956861197536f9ceeaf63d6b3.tar.zst
astro-7461e82c81438df956861197536f9ceeaf63d6b3.zip
Add "allowHTML" option for Markdoc with HTML parsing/processing (#7597)
* 7576 - initial support for HTML inside Markdoc. This uses htmlparser2 to perform a pure token transform/mutation on the markdown-it tokens, replacing the original raw HTML string tokens with a richer set of tokens per HTML node, and in the process Markdoc tags are interleaved in the resulting token graph at the appropriate locations This removes the legacy config of the @astrojs/markdoc integration entirely (suggested by @bholmesdev) and introduces a new type for options to be specified in the astro config, initially, with just the new "enableHTML" option When "enableHTML" is *not* enabled (the default), the behavior of the entire @astrojs/markdoc integration should remain functionally equivalent to before this change * 7576 - fixed issues with whitespace preservation also: * cleaned up " to ' for astro project preferred linting * made the html rendering test fixture use a dynamic path * 7576 - detailed nested HTML test coverage * 7576 - component + HTML interleaved tests * 7576 - fix lint problems from previous changes * 7576 - some commentary * 7576 - file naming, refactor html under imports, package.json exports definition for html * 7576 * move out of extensions dir, remove export * cdata handling changes * 7576 * inline license from third party code * cleanup test class copy of HTML output * remove // third party indicators for imports (clarification: not third party code, just a indicator this group of imports is third party) * 7576 - fixed test before/after for DRY'ness * 7576 - no need to React-ify HTML attribute case * 7576 - rename "enableHTML" option to "allowHTML" * Added Markdoc allowHTML feature changeset * 7576 - updated README with allowHTML info * 7576 - fixed changeset typo * 7576 - minor edits based on PR feedback for docs * 7576 - minor edits based on PR feedback for docs
Diffstat (limited to 'packages/integrations/markdoc/src/html/css/style-to-object.ts')
-rw-r--r--packages/integrations/markdoc/src/html/css/style-to-object.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/packages/integrations/markdoc/src/html/css/style-to-object.ts b/packages/integrations/markdoc/src/html/css/style-to-object.ts
new file mode 100644
index 000000000..7febe3152
--- /dev/null
+++ b/packages/integrations/markdoc/src/html/css/style-to-object.ts
@@ -0,0 +1,70 @@
+// @ts-nocheck
+// https://github.com/remarkablemark/style-to-object
+
+/**
+ * @license MIT
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Menglin "Mark" Xu <mark@remarkablemark.org>
+ *
+ * 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.
+ */
+
+import { parseInlineStyles } from "./parse-inline-styles.js";
+
+/**
+ * Parses inline style to object.
+ *
+ * @example
+ * // returns { 'line-height': '42' }
+ * styleToObject('line-height: 42;');
+ *
+ * @param {String} style - The inline style.
+ * @param {Function} [iterator] - The iterator function.
+ * @return {null|Object}
+ */
+export function styleToObject(style, iterator) {
+ let output = null;
+ if (!style || typeof style !== 'string') {
+ return output;
+ }
+
+ let declaration;
+ let declarations = parseInlineStyles(style);
+ let hasIterator = typeof iterator === 'function';
+ let property;
+ let value;
+
+ for (let i = 0, len = declarations.length; i < len; i++) {
+ declaration = declarations[i];
+ property = declaration.property;
+ value = declaration.value;
+
+ if (hasIterator) {
+ iterator(property, value, declaration);
+ } else if (value) {
+ output || (output = {});
+ output[property] = value;
+ }
+ }
+
+ return output;
+}