-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4043 from patrick-hertling/feature/code-block-jso…
…n-format json formatting in code-block plugin
- Loading branch information
Showing
8 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@udecode/plate-code-block': minor | ||
--- | ||
|
||
Ability to format a valid JSON string in a code block. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { Editor } from '@udecode/plate'; | ||
|
||
import type { TCodeBlockElement } from '../types'; | ||
|
||
import { formatJson, isValidJson } from './jsonFormatter'; | ||
|
||
const supportedLanguages = new Set(['json']); | ||
|
||
export const isLangSupported = (lang?: string): boolean => | ||
Boolean(lang && supportedLanguages.has(lang)); | ||
|
||
export const isValidSyntax = (code: string, lang?: string): boolean => { | ||
if (!isLangSupported(lang)) { | ||
return false; | ||
} | ||
|
||
switch (lang) { | ||
case 'json': { | ||
return isValidJson(code); | ||
} | ||
default: { | ||
return false; | ||
} | ||
} | ||
}; | ||
|
||
export const formatCodeBlock = ( | ||
editor: Editor, | ||
{ | ||
element, | ||
}: { | ||
element: TCodeBlockElement; | ||
} | ||
) => { | ||
const { lang } = element; | ||
|
||
if (!lang || !isLangSupported(lang)) { | ||
return; | ||
} | ||
|
||
const code = editor.api.string(element); | ||
|
||
if (isValidSyntax(code, lang)) { | ||
const formattedCode = formatCode(code, lang); | ||
editor.tf.insertText(formattedCode, { at: element }); | ||
} | ||
}; | ||
|
||
const formatCode = (code: string, lang?: string): string => { | ||
switch (lang) { | ||
case 'json': { | ||
return formatJson(code); | ||
} | ||
default: { | ||
return code; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @file Automatically generated by barrelsby. | ||
*/ | ||
|
||
export * from './formatter'; | ||
export * from './jsonFormatter'; |
31 changes: 31 additions & 0 deletions
31
packages/code-block/src/lib/formatter/jsonFormatter.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { formatJson, isValidJson } from './jsonFormatter'; | ||
|
||
describe('JsonFormatter', () => { | ||
it('should detect valid JSON', () => { | ||
const json = '{ "name": "ChatGPT", "type": "AI" }'; | ||
const isValid = isValidJson(json); | ||
expect(isValid).toBe(true); | ||
}); | ||
|
||
it('should detect invalid JSON', () => { | ||
const json = '{ name: "ChatGPT", type: AI }'; | ||
const isValid = isValidJson(json); | ||
expect(isValid).toBe(false); | ||
}); | ||
|
||
it('should format JSON', () => { | ||
const json = '{"name":"ChatGPT","type":"AI"}'; | ||
const formattedJson = formatJson(json); | ||
const expected = `{ | ||
"name": "ChatGPT", | ||
"type": "AI" | ||
}`; | ||
expect(formattedJson).toBe(expected); | ||
}); | ||
|
||
it('should not format invalid JSON', () => { | ||
const json = '{ name: "ChatGPT", type: AI }'; | ||
const formattedJson = formatJson(json); | ||
expect(formattedJson).toBe(json); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export const formatJson = (code: string): string => { | ||
try { | ||
return JSON.stringify(JSON.parse(code), null, 2); | ||
} catch (error) { | ||
return code; | ||
} | ||
}; | ||
|
||
export const isValidJson = (code: string): boolean => { | ||
try { | ||
JSON.parse(code); | ||
|
||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters