-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(table): make the parsing of cell class stricter (#444)
- Loading branch information
Showing
5 changed files
with
146 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Parse the markdown-attrs format to retrieve a class name | ||
* Putting all the requirements in regex was more complicated than parsing a string char by char. | ||
* | ||
* @param {string} inputString - The string to parse. | ||
* @returns {string|null} - The extracted class or null if there is none | ||
*/ | ||
|
||
export function parseAttrsClass(inputString: string): string | null { | ||
const validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .=-_'; | ||
|
||
if (!inputString.startsWith('{')) { | ||
return null; | ||
} | ||
|
||
for (let i = 1; i < inputString.length; i++) { | ||
const char = inputString[i]; | ||
|
||
if (char === '}') { | ||
const contentInside = inputString.slice(1, i).trim(); // content excluding { and } | ||
|
||
if (!contentInside) { | ||
return null; | ||
} | ||
|
||
const parts = contentInside.split('.'); | ||
if (parts.length !== 2 || !parts[1]) { | ||
return null; | ||
} | ||
//There should be a preceding whitespace | ||
if (!parts[0].endsWith(' ') && parts[0] !== '') { | ||
return null; | ||
} | ||
|
||
return parts[1]; | ||
} | ||
|
||
if (!validChars.includes(char)) { | ||
return null; | ||
} | ||
} | ||
|
||
return null; | ||
} |
File renamed without changes.
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,29 @@ | ||
import {parseAttrsClass} from '../../src/transform/plugins/table/utils'; | ||
|
||
describe('parseAttrsClass', () => { | ||
it('should correctly parse a class in markdown attrs format', () => { | ||
expect(parseAttrsClass('{property=value .class}')).toEqual('class'); | ||
}); | ||
|
||
it('should correctly parse a class when its the only property', () => { | ||
expect(parseAttrsClass('{.class}')).toEqual('class'); | ||
}); | ||
|
||
it('should require a whitespace if there are other properties', () => { | ||
expect(parseAttrsClass('{property=value.class}')).toEqual(null); | ||
}); | ||
|
||
it('should bail if there are unexpected symbols', () => { | ||
expect(parseAttrsClass('{property="value" .class}')).toEqual(null); | ||
}); | ||
|
||
it('should allow a dash in the class name', () => { | ||
expect(parseAttrsClass('{.cell-align-center}')).toEqual('cell-align-center'); | ||
}); | ||
|
||
it('should not touch includes', () => { | ||
expect( | ||
parseAttrsClass('{% include <a href="./mocks/include.md">create-folder</a> %}'), | ||
).toEqual(null); | ||
}); | ||
}); |