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

Shift trailing whitespace outside of inline mdast nodes #5

Closed
danburzo opened this issue Aug 7, 2023 · 1 comment
Closed

Shift trailing whitespace outside of inline mdast nodes #5

danburzo opened this issue Aug 7, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@danburzo
Copy link
Owner

danburzo commented Aug 7, 2023

For example, this HTML:

<span style="font-weight: 400;">Select the </span><strong>Project Window </strong><span style="font-weight: 400;">in your Dashboard.</span>

Produces this Markdown:

Select the __Project Window __in your Dashboard.

Add a mdast-level util to shift the trailing whitespace to the outside of inline (emphasis) nodes.

@danburzo danburzo self-assigned this Aug 7, 2023
@danburzo danburzo added the enhancement New feature or request label Aug 7, 2023
@danburzo
Copy link
Owner Author

A basic implementation, based on ideas in this discussion:

/*
	Remark plugin to fix trailing whitespace in inline nodes.
	See: https://github.com/orgs/syntax-tree/discussions/60
*/
function remarkTrailingWhitespace() {
	return function (tree, file) {
		visit(tree, ['strong', 'emphasis'], (node, index, parent) => {
			// Remove empty nodes
			if (!node.children.length) {
				parent.children.splice(index, 1);
				return SKIP;
			}

			const first = node.children[0];

			// Remove image nodes from within emphasis nodes
			if (first.type === 'image') {
				parent.children.splice(index, 1, first);
				return SKIP;
			}

			// Remove trailing whitespace at the beginning
			if (first.type === 'text') {
				const start_match = first.value.match(/^(\s+)(.*)/);
				if (start_match) {
					const [_, start_ws, start_text] = start_match;
					parent.children.splice(index, 0, {
						type: 'text',
						value: start_ws
					});
					if (start_text) {
						// has non-whitespace content
						first.value = start_text;
					} else {
						// all-whitespace content
						node.children.shift();
					}
					// Re-visit this node
					return [SKIP, index - 1];
				}
			}

			const last = node.children[node.children.length - 1];
			if (last.type === 'text') {
				const end_match = last.value.match(/(.*?)(\s+)$/);
				if (end_match) {
					const [_, end_text, end_ws] = end_match;
					parent.children.splice(index + 1, 0, {
						type: 'text',
						value: end_ws
					});
					if (end_text) {
						last.value = end_text;
					} else {
						node.children.pop();
					}
				}
			}

			return SKIP;
		});
	};
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant