Skip to content

masking option #17

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

Closed
azu opened this issue Feb 10, 2023 · 2 comments · Fixed by #18
Closed

masking option #17

azu opened this issue Feb 10, 2023 · 2 comments · Fixed by #18
Labels
Status: Proposal Request for comments

Comments

@azu
Copy link
Member

azu commented Feb 10, 2023

Sometimes, we want to mask Code node in Paragraph node.
In almost case, textlint rules do not want to handle Code node.

Example

const source = new StringSource(AST);
const result = source.toString({
  replacer: (node, index) => {
    if (node.type === "Code") {
      return "_".repeat(node.value.length)
    }
    return node
  },
});

Inspired by JSON.stringify() - JavaScript | MDN

@azu azu added the Status: Proposal Request for comments label Feb 10, 2023
@azu
Copy link
Member Author

azu commented Feb 10, 2023

This API works, but it is unsafe.
value length is changed, original* method will not work correct.

@azu
Copy link
Member Author

azu commented Feb 10, 2023

Supported command:

  • delete node
  • mask value
    describe("replacer option", function () {
        it("can modify specify node", function () {
            const originalText = "This is `code`.";
            const AST = parse(originalText);
            const source = new StringSource(AST, {
                replacer({ node, maskValue }) {
                    if (node.type === "Code") {
                        return maskValue("_");
                    }
                    return;
                }
            });
            assert.strictEqual(source.toString(), "This is ____.");
        });
        it("modify and get original index", function () {
            const AST = parse("This is `TEST`.");
            const source = new StringSource(AST, {
                replacer({ node, deleteNode }) {
                    if (node.type === "Code") {
                        return deleteNode();
                    }
                    return;
                }
            });
            assert.strictEqual(source.toString(), "This is .");
        });
    })

@azu azu linked a pull request Feb 10, 2023 that will close this issue
@azu azu closed this as completed in #18 Feb 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Proposal Request for comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant