Skip to content

Commit

Permalink
Trim code inline spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Mar 5, 2024
1 parent 5372296 commit 93b886e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ smd.parser_end(parser)
- [x] with many backticks
- [x] `` `inline code` `` with backticks
- [x] with many backticks
- [ ] trim symmetrical spaces ` code `
- [x] trim spaces ` code `
- [x] *italic* with single asterisks
- [x] **Bold** with double asterisks
- [x] _italic_ with underscores
Expand Down
20 changes: 13 additions & 7 deletions smd.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,9 @@ export function parser_write(p, chunk) {
*/
else {
parser_add_token(p, PARAGRAPH)
parser_add_token(p, CODE_INLINE)
p.text = p.pending.slice(p.backticks_count)
p.pending = ""
parser_write(p, char)
p.backticks_count = 0
parser_write(p, pending_with_char)
}
continue
case '\n':
Expand Down Expand Up @@ -513,7 +512,9 @@ export function parser_write(p, chunk) {
case CODE_INLINE:
switch (char) {
case '`':
if (pending_with_char.length === p.backticks_count) {
if (pending_with_char.length ===
p.backticks_count + Number(p.pending[0] === ' ') // 0 or 1 for space
) {
parser_add_text(p)
parser_end_token(p)
p.pending = ""
Expand All @@ -527,6 +528,11 @@ export function parser_write(p, chunk) {
p.pending = ""
_parser_into_line_break(p)
continue
/* Trim space before ` */
case ' ':
p.text += p.pending
p.pending = char
continue
default:
p.text += pending_with_char
p.pending = ""
Expand Down Expand Up @@ -694,7 +700,7 @@ export function parser_write(p, chunk) {
p.backticks_count += 1 // started at 0, and first wasn't counted
parser_add_text(p)
parser_add_token(p, CODE_INLINE)
p.text = char
p.text = ' ' === char || '\n' === char ? "" : char // trim leading space
p.pending = ""
}
continue
Expand Down Expand Up @@ -819,7 +825,7 @@ export function parser_write(p, chunk) {
* @typedef {object} Default_Renderer_Data
* @property {HTMLElement[]} nodes
* @property {number } index
*
*
* @typedef {Renderer <Default_Renderer_Data>} Default_Renderer
* @typedef {Renderer_Add_Token<Default_Renderer_Data>} Default_Renderer_Add_Token
* @typedef {Renderer_End_Token<Default_Renderer_Data>} Default_Renderer_End_Token
Expand Down Expand Up @@ -899,7 +905,7 @@ export function default_set_attr(data, type, value) {

/**
* @typedef {undefined} Logger_Renderer_Data
*
*
* @typedef {Renderer <Logger_Renderer_Data>} Logger_Renderer
* @typedef {Renderer_Add_Token<Logger_Renderer_Data>} Logger_Renderer_Add_Token
* @typedef {Renderer_End_Token<Logger_Renderer_Data>} Logger_Renderer_End_Token
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,17 @@ for (let l = 1; l <= 4; l += 1) {
}]
)

test_single_write("Code Inline trims spaces" + " - "+l+" backticks",
c + " a " + c,
[{
type : smd.Token.Paragraph,
children: [{
type : smd.Token.Code_Inline,
children: ["a"]
}],
}]
)

test_single_write("Code Inline x2" + " - "+l+" backticks",
c+"a"+c+" "+c+"b"+c,
[{
Expand Down

0 comments on commit 93b886e

Please sign in to comment.