remove unwanted spacing around the list in block editor (#19521)

* remove unwanted spacing around the list in block editor

* fix spacing issue

---------

Co-authored-by: karanh37 <karanh37@gmail.com>
This commit is contained in:
Ashish Gupta 2025-01-28 09:34:11 +05:30 committed by GitHub
parent 33c107dea0
commit 13f3d19b5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 2 deletions

View File

@ -10,7 +10,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getTextFromHtmlString } from './BlockEditorUtils';
import {
getHtmlStringFromMarkdownString,
getTextFromHtmlString,
} from './BlockEditorUtils';
describe('getTextFromHtmlString', () => {
it('should return empty string when input is undefined', () => {
@ -72,3 +75,45 @@ describe('getTextFromHtmlString', () => {
expect(getTextFromHtmlString(input)).toBe(output);
});
});
describe('getHtmlStringFromMarkdownString', () => {
it('should return the same string if input is already HTML', () => {
const input = '<p>Hello World</p>';
expect(getHtmlStringFromMarkdownString(input)).toBe(input);
});
it('should convert markdown to HTML', () => {
const input = 'Hello **World**';
const expectedOutput = '<p>Hello <strong>World</strong></p>';
expect(getHtmlStringFromMarkdownString(input)).toBe(expectedOutput);
});
it('should handle empty string', () => {
expect(getHtmlStringFromMarkdownString('')).toBe('');
});
it('should preserve special characters in markdown', () => {
const input = 'Hello & World! @ #$%^';
const expectedOutput = '<p>Hello &amp; World! @ #$%^</p>';
expect(getHtmlStringFromMarkdownString(input)).toBe(expectedOutput);
});
it('should handle complex markdown structure', () => {
const input = `
## Demo Title
Small Subtitle.
- Item 1
- Item 2
`;
const expectedOutput = `
<pre><code>##DemoTitleSmallSubtitle.-Item1-Item2</code></pre>
`;
expect(getHtmlStringFromMarkdownString(input).replace(/\s+/g, '')).toBe(
expectedOutput.replace(/\s+/g, '')
);
});
});

View File

@ -139,6 +139,12 @@ const _convertMarkdownStringToHtmlString = new Showdown.Converter({
ellipsis: false,
});
export const getHtmlStringFromMarkdownString = (content: string) => {
return isHTMLString(content)
? content
: _convertMarkdownStringToHtmlString.makeHtml(content);
};
/**
* Set the content of the editor
* @param editor The editor instance
@ -146,7 +152,7 @@ const _convertMarkdownStringToHtmlString = new Showdown.Converter({
*/
export const setEditorContent = (editor: Editor, newContent: string) => {
// Convert the markdown string to an HTML string
const htmlString = _convertMarkdownStringToHtmlString.makeHtml(newContent);
const htmlString = getHtmlStringFromMarkdownString(newContent);
editor.commands.setContent(htmlString);