Skip to content

Commit

Permalink
Updates deps, fixes #5 trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo committed Oct 29, 2024
1 parent 65c1b80 commit 86f396f
Show file tree
Hide file tree
Showing 4 changed files with 746 additions and 271 deletions.
96 changes: 11 additions & 85 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,70 +19,6 @@ function noop() {
return () => {};
}

/*
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;
});
};
}

export async function slurp(stream) {
let arr = [],
len = 0;
Expand Down Expand Up @@ -146,16 +82,11 @@ function rehypeToText() {
}

export function getHtmlToMdProcessor(opts = {}) {
return (
unified()
.use(rehypeParse)
.use(rehypeRemark)
.use(remarkGfm)
// Not enabled yet, as it doesn’t treat some corner cases
// See: https://github.com/danburzo/trimd/issues/5
// .use(remarkTrailingWhitespace)
.use(remarkStringify, opts)
);
return unified()
.use(rehypeParse)
.use(rehypeRemark)
.use(remarkGfm)
.use(remarkStringify, opts);
}

export function getMdToMdProcessor(opts = {}, transformFn) {
Expand All @@ -180,17 +111,12 @@ export function getMdToHtmlProcessor(opts = {}) {
}

export function getHtmlToHtmlProcessor(opts = {}) {
return (
unified()
.use(rehypeParse)
.use(rehypeRemark)
// Not enabled yet, as it doesn’t treat some corner cases
// See: https://github.com/danburzo/trimd/issues/5
// .use(remarkTrailingWhitespace)
.use(remarkRehype)
.use(opts.sanitize ? rehypeSanitize : noop)
.use(rehypeStringify)
);
return unified()
.use(rehypeParse)
.use(rehypeRemark)
.use(remarkRehype)
.use(opts.sanitize ? rehypeSanitize : noop)
.use(rehypeStringify);
}

export function getMdToTextProcessor() {
Expand Down
Loading

0 comments on commit 86f396f

Please sign in to comment.