-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(new tool): xml formatter (#457)
* feat(new tool): xml formatter * feat(xml-formatter): added happy path e2e tests * refactor(xml-formatter): improved unit tests * refactor(xml-formatter): add better suitable icon * feat(xml-formatter): added happy path e2e tests * feat(xml-formatter): registered xml as syntax highlighter * chore(auto-import): removed unused NSpace --------- Co-authored-by: Corentin Thomasset <[email protected]>
- Loading branch information
1 parent
f771e7a
commit a6bbeae
Showing
10 changed files
with
156 additions
and
0 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,12 @@ | ||
import { Code } from '@vicons/tabler'; | ||
import { defineTool } from '../tool'; | ||
|
||
export const tool = defineTool({ | ||
name: 'XML formatter', | ||
path: '/xml-formatter', | ||
description: 'Prettify your XML string to a human friendly readable format.', | ||
keywords: ['xml', 'prettify', 'format'], | ||
component: () => import('./xml-formatter.vue'), | ||
icon: Code, | ||
createdAt: new Date('2023-06-17'), | ||
}); |
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,23 @@ | ||
import { expect, test } from '@playwright/test'; | ||
|
||
test.describe('Tool - XML formatter', () => { | ||
test.beforeEach(async ({ page }) => { | ||
await page.goto('/xml-formatter'); | ||
}); | ||
|
||
test('Has correct title', async ({ page }) => { | ||
await expect(page).toHaveTitle('XML formatter - IT Tools'); | ||
}); | ||
|
||
test('XML is converted into a human readable format', async ({ page }) => { | ||
await page.getByTestId('input').fill('<foo><bar>baz</bar><bar>baz</bar></foo>'); | ||
|
||
const formattedXml = await page.getByTestId('area-content').innerText(); | ||
|
||
expect(formattedXml.trim()).toEqual(` | ||
<foo> | ||
<bar>baz</bar> | ||
<bar>baz</bar> | ||
</foo>`.trim()); | ||
}); | ||
}); |
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,27 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { formatXml } from './xml-formatter.service'; | ||
|
||
describe('xml-formatter service', () => { | ||
describe('formatXml', () => { | ||
it('converts XML into a human readable format', () => { | ||
const initString = '<hello><world>foo</world><world>bar</world></hello>'; | ||
|
||
expect(formatXml(initString)).toMatchInlineSnapshot(` | ||
"<hello> | ||
<world> | ||
foo | ||
</world> | ||
<world> | ||
bar | ||
</world> | ||
</hello>" | ||
`); | ||
}); | ||
|
||
it('returns an empty string if the input is not valid XML', () => { | ||
const initString = 'hello world'; | ||
|
||
expect(formatXml(initString)).toEqual(''); | ||
}); | ||
}); | ||
}); |
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,28 @@ | ||
import xmlFormat, { type XMLFormatterOptions } from 'xml-formatter'; | ||
import { withDefaultOnError } from '@/utils/defaults'; | ||
|
||
export { formatXml, isValidXML }; | ||
|
||
function cleanRawXml(rawXml: string): string { | ||
return rawXml.trim(); | ||
} | ||
|
||
function formatXml(rawXml: string, options?: XMLFormatterOptions): string { | ||
return withDefaultOnError(() => xmlFormat(cleanRawXml(rawXml), options) ?? '', ''); | ||
} | ||
|
||
function isValidXML(rawXml: string): boolean { | ||
const cleanedRawXml = cleanRawXml(rawXml); | ||
|
||
if (cleanedRawXml === '') { | ||
return true; | ||
} | ||
|
||
try { | ||
xmlFormat(cleanedRawXml); | ||
return true; | ||
} | ||
catch (e) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<script setup lang="ts"> | ||
import { formatXml, isValidXML } from './xml-formatter.service'; | ||
import type { UseValidationRule } from '@/composable/validation'; | ||
const defaultValue = '<hello><world>foo</world><world>bar</world></hello>'; | ||
const indentSize = useStorage('xml-formatter:indent-size', 2); | ||
const collapseContent = useStorage('xml-formatter:collapse-content', true); | ||
function transformer(value: string) { | ||
return formatXml(value, { | ||
indentation: ' '.repeat(indentSize.value), | ||
collapseContent: collapseContent.value, | ||
lineSeparator: '\n', | ||
}); | ||
} | ||
const rules: UseValidationRule<string>[] = [ | ||
{ | ||
validator: isValidXML, | ||
message: 'Provided XML is not valid.', | ||
}, | ||
]; | ||
</script> | ||
|
||
<template> | ||
<div important:flex-full important:flex-shrink-0 important:flex-grow-0> | ||
<div flex justify-center> | ||
<n-form-item label="Collapse content:" label-placement="left"> | ||
<n-switch v-model:value="collapseContent" /> | ||
</n-form-item> | ||
<n-form-item label="Indent size:" label-placement="left" label-width="100" :show-feedback="false"> | ||
<n-input-number v-model:value="indentSize" min="0" max="10" w-100px /> | ||
</n-form-item> | ||
</div> | ||
</div> | ||
|
||
<format-transformer | ||
input-label="Your XML" | ||
input-placeholder="Paste your XML here..." | ||
output-label="Formatted XML from your XML" | ||
output-language="xml" | ||
:input-validation-rules="rules" | ||
:transformer="transformer" | ||
:input-default="defaultValue" | ||
/> | ||
</template> |
a6bbeae
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.
Successfully deployed to the following URLs:
it-tools – ./
it-tools-ctmsst.vercel.app
it-tools.vercel.app
it-tools-git-main-ctmsst.vercel.app
it-tools.tech