-
Notifications
You must be signed in to change notification settings - Fork 882
[EuiMarkdownFormat] Fix intraword underscore emphasis rendering #9408
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
Changes from 3 commits
b9289e5
44864e7
b95456e
366c5f4
447b862
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| **Bug fixes** | ||
|
|
||
| - Fixed support for intraword underscores in `EuiMarkdownFormat` | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { render } from '../../../../test/rtl'; | ||
| import { EuiMarkdownFormat } from '../../index'; | ||
|
|
||
| describe('remarkIntrawordUnderscore', () => { | ||
| it('preserves identifiers with double underscores as plain text', () => { | ||
| const { container } = render( | ||
| <EuiMarkdownFormat> | ||
| {`ABDC__AppleBanana__c | ||
| ABDC__MangoKiwi__c | ||
| ABDC__PineappleCherry__c`} | ||
| </EuiMarkdownFormat> | ||
| ); | ||
|
|
||
| expect(container.querySelector('strong')).not.toBeInTheDocument(); | ||
| expect(container).toHaveTextContent('ABDC__AppleBanana__c'); | ||
| expect(container).toHaveTextContent('ABDC__MangoKiwi__c'); | ||
| expect(container).toHaveTextContent('ABDC__PineappleCherry__c'); | ||
| }); | ||
|
|
||
| it('preserves identifiers with single underscores as plain text', () => { | ||
| const { container } = render( | ||
| <EuiMarkdownFormat>{'some_variable_name'}</EuiMarkdownFormat> | ||
| ); | ||
|
|
||
| expect(container.querySelector('em')).not.toBeInTheDocument(); | ||
| expect(container).toHaveTextContent('some_variable_name'); | ||
| }); | ||
|
|
||
| it('still applies bold for standalone double underscores', () => { | ||
| const { container } = render( | ||
| <EuiMarkdownFormat>{'__bold text__'}</EuiMarkdownFormat> | ||
| ); | ||
|
|
||
| expect(container.querySelector('strong')).toHaveTextContent('bold text'); | ||
| }); | ||
|
|
||
| it('still applies emphasis for standalone single underscores', () => { | ||
| const { container } = render( | ||
| <EuiMarkdownFormat>{'_italic text_'}</EuiMarkdownFormat> | ||
| ); | ||
|
|
||
| expect(container.querySelector('em')).toHaveTextContent('italic text'); | ||
| }); | ||
|
|
||
| it('handles multiple identifiers in a sentence', () => { | ||
| const { container } = render( | ||
| <EuiMarkdownFormat> | ||
| {'Fields ABDC__AppleBanana__c and ABDC__MangoKiwi__c are required'} | ||
| </EuiMarkdownFormat> | ||
| ); | ||
|
|
||
| expect(container.querySelector('strong')).not.toBeInTheDocument(); | ||
| expect(container).toHaveTextContent( | ||
| 'Fields ABDC__AppleBanana__c and ABDC__MangoKiwi__c are required' | ||
| ); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,151 @@ | ||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||||||||||||||||||||||||||||
| * or more contributor license agreements. Licensed under the Elastic License | ||||||||||||||||||||||||||||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||||||||||||||||||||||||||||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||||||||||||||||||||||||||||
| * Side Public License, v 1. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Temporary workaround for https://github.com/elastic/eui/issues/9404 | ||||||||||||||||||||||||||||
| // remark-parse v8 doesn't implement the CommonMark rule that underscore | ||||||||||||||||||||||||||||
| // emphasis delimiters flanked by alphanumerics on both sides (intraword) | ||||||||||||||||||||||||||||
| // cannot open or close emphasis. This causes identifiers like | ||||||||||||||||||||||||||||
| // `ABCD__PineappleCherry__e` to render with bold formatting. | ||||||||||||||||||||||||||||
| // This plugin walks the parsed AST and reverses incorrectly applied | ||||||||||||||||||||||||||||
| // emphasis/strong nodes when both sides are alphanumeric characters. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import { Plugin } from 'unified'; | ||||||||||||||||||||||||||||
| // eslint-disable-next-line import/no-unresolved | ||||||||||||||||||||||||||||
| import { Node, Parent } from 'unist'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| interface TextNode extends Node { | ||||||||||||||||||||||||||||
| type: 'text'; | ||||||||||||||||||||||||||||
| value: string; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| interface EmphasisOrStrong extends Parent { | ||||||||||||||||||||||||||||
| type: 'emphasis' | 'strong'; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const isTextNode = (node: Node): node is TextNode => node.type === 'text'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const isEmphasisOrStrong = (node: Node): node is EmphasisOrStrong => | ||||||||||||||||||||||||||||
| node.type === 'emphasis' || node.type === 'strong'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const isAlphanumeric = (ch: string): boolean => /[a-zA-Z0-9]/.test(ch); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Recursively converts an emphasis/strong node back into plain text | ||||||||||||||||||||||||||||
| * with underscore delimiters restored. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| function flattenToText(node: EmphasisOrStrong): string { | ||||||||||||||||||||||||||||
| const delimiter = node.type === 'emphasis' ? '_' : '__'; | ||||||||||||||||||||||||||||
| let inner = ''; | ||||||||||||||||||||||||||||
| for (const child of node.children) { | ||||||||||||||||||||||||||||
| if (isTextNode(child)) { | ||||||||||||||||||||||||||||
| inner += child.value; | ||||||||||||||||||||||||||||
| } else if (isEmphasisOrStrong(child)) { | ||||||||||||||||||||||||||||
| inner += flattenToText(child); | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| // Contains non-text children (links, images, etc.) — leave emphasis intact | ||||||||||||||||||||||||||||
| return delimiter + collectText(node) + delimiter; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return delimiter + inner + delimiter; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function collectText(node: Node): string { | ||||||||||||||||||||||||||||
| if (isTextNode(node)) return node.value; | ||||||||||||||||||||||||||||
| if ('children' in node) { | ||||||||||||||||||||||||||||
| return (node as Parent).children.map(collectText).join(''); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return ''; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function processParent(parent: Parent, source: string) { | ||||||||||||||||||||||||||||
| let modified = false; | ||||||||||||||||||||||||||||
| let i = 0; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| while (i < parent.children.length) { | ||||||||||||||||||||||||||||
| const child = parent.children[i]; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (isEmphasisOrStrong(child) && isIntraword(parent, i, source)) { | ||||||||||||||||||||||||||||
| const textValue = flattenToText(child); | ||||||||||||||||||||||||||||
| const replacement: TextNode = { | ||||||||||||||||||||||||||||
| type: 'text', | ||||||||||||||||||||||||||||
| value: textValue, | ||||||||||||||||||||||||||||
| } as TextNode; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| parent.children.splice(i, 1, replacement); | ||||||||||||||||||||||||||||
| modified = true; | ||||||||||||||||||||||||||||
|
Comment on lines
+72
to
+80
|
||||||||||||||||||||||||||||
| // Don't advance — the replaced node may need to merge with neighbors | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| if ('children' in child) { | ||||||||||||||||||||||||||||
| processParent(child as Parent, source); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| i++; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (modified) { | ||||||||||||||||||||||||||||
| mergeAdjacentText(parent); | ||||||||||||||||||||||||||||
| modified = false; | ||||||||||||||||||||||||||||
| // After merging, restart scan since indices shifted | ||||||||||||||||||||||||||||
| i = 0; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Checks whether the emphasis/strong node at `index` within `parent` | ||||||||||||||||||||||||||||
| * is an intraword usage of underscore delimiters. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| function isIntraword(parent: Parent, index: number, source: string): boolean { | ||||||||||||||||||||||||||||
| const node = parent.children[index]; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Verify the delimiter is `_` (not `*`) by inspecting the source | ||||||||||||||||||||||||||||
| if (node.position?.start?.offset != null) { | ||||||||||||||||||||||||||||
| const ch = source[node.position.start.offset]; | ||||||||||||||||||||||||||||
| if (ch !== '_') return false; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const prev = index > 0 ? parent.children[index - 1] : null; | ||||||||||||||||||||||||||||
| const next = | ||||||||||||||||||||||||||||
| index < parent.children.length - 1 ? parent.children[index + 1] : null; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const prevEndsWithAlpha = | ||||||||||||||||||||||||||||
| prev != null && | ||||||||||||||||||||||||||||
| isTextNode(prev) && | ||||||||||||||||||||||||||||
| prev.value.length > 0 && | ||||||||||||||||||||||||||||
| isAlphanumeric(prev.value[prev.value.length - 1]); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const nextStartsWithAlpha = | ||||||||||||||||||||||||||||
| next != null && | ||||||||||||||||||||||||||||
| isTextNode(next) && | ||||||||||||||||||||||||||||
| next.value.length > 0 && | ||||||||||||||||||||||||||||
| isAlphanumeric(next.value[0]); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return prevEndsWithAlpha && nextStartsWithAlpha; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function mergeAdjacentText(parent: Parent) { | ||||||||||||||||||||||||||||
| let i = 0; | ||||||||||||||||||||||||||||
| while (i < parent.children.length - 1) { | ||||||||||||||||||||||||||||
| if (isTextNode(parent.children[i]) && isTextNode(parent.children[i + 1])) { | ||||||||||||||||||||||||||||
| (parent.children[i] as TextNode).value += ( | ||||||||||||||||||||||||||||
| parent.children[i + 1] as TextNode | ||||||||||||||||||||||||||||
| ).value; | ||||||||||||||||||||||||||||
|
Comment on lines
+177
to
+179
|
||||||||||||||||||||||||||||
| (parent.children[i] as TextNode).value += ( | |
| parent.children[i + 1] as TextNode | |
| ).value; | |
| const current = parent.children[i] as TextNode & { position?: any }; | |
| const next = parent.children[i + 1] as TextNode & { position?: any }; | |
| current.value += next.value; | |
| // Preserve and extend positional information so the merged node | |
| // covers the full text span of both original nodes. | |
| if (current.position && next.position && next.position.end) { | |
| current.position.end = next.position.end; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flattenToText()claims to "leave emphasis intact" when encountering non-text children, but it still returns a string andprocessParent()always replaces the node with atextnode. This will strip nested nodes likeinlineCode,link, etc. and can even drop their content (e.g.inlineCodehasvaluebut nochildren, socollectText()returns an empty string). Consider bailing out of the replacement when non-text descendants are present (e.g. haveflattenToTextreturnnulland skipsplice), or otherwise ensure all literal node types are preserved and non-text nodes are not lost.