-
-
Notifications
You must be signed in to change notification settings - Fork 62
Add support for editorjs list 2.0 and improve image translation #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,30 @@ | ||
import { OutputBlockData } from "@editorjs/editorjs"; | ||
|
||
export const image = ({ data }: OutputBlockData): string => { | ||
const caption = data.caption ? data.caption : "Image"; | ||
const url = data?.file?.url || data?.url; | ||
const url = data.file?.url || data.url || ""; | ||
const caption = data.caption || ""; | ||
const isCaptionBlank = !caption || caption.trim() === ""; | ||
|
||
return `<img src="${url}" alt="${caption}" />`; | ||
return `<table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%"> | ||
<tr> | ||
<td align="center" style="padding-top:20px;${isCaptionBlank ? "padding-bottom:20px;" : "padding-bottom:5px;"}"> | ||
<img | ||
src="${url}" | ||
alt="${caption}" | ||
width="500" | ||
height="auto" | ||
style="display:block; max-width:500px; width:100%; height:auto; border:0; outline:none; text-decoration:none;" | ||
/> | ||
</td> | ||
</tr> | ||
${ | ||
!isCaptionBlank | ||
? `<tr> | ||
<td align="center" style="padding-bottom:20px;"> | ||
<p style="margin:0; font-size:14px; line-height:1; color:#999999; text-align:center;">${caption}</p> | ||
</td> | ||
</tr>` | ||
: "" | ||
} | ||
</table>`; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,70 @@ | ||
import { OutputBlockData } from "@editorjs/editorjs"; | ||
|
||
export const list = ({ data }: OutputBlockData) => { | ||
const listStyle = data.style === "unordered" ? "ul" : "ol"; | ||
let listStyle = "ul"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use |
||
if (data.style === "ordered") { | ||
listStyle = "ol"; | ||
} | ||
|
||
const recursor = (items: any, listStyle: string) => { | ||
if (!items || !items.length) { | ||
return ""; | ||
} | ||
|
||
const list = items.map((item: any) => { | ||
if (!item.content && !item.items) return `<li>${item}</li>`; | ||
// If it's legacy format (just strings) | ||
if (typeof item === "string") { | ||
return `<li>${item}</li>`; | ||
} | ||
|
||
// For v2.0 format with content property | ||
let content = item.content || ""; | ||
let nestedList = ""; | ||
if (item.items?.length) nestedList = recursor(item.items, listStyle); | ||
if (item.content) return `<li>${item.content}${nestedList}</li>`; | ||
|
||
if (item.items && item.items.length) { | ||
nestedList = recursor(item.items, listStyle); | ||
} | ||
|
||
if ( | ||
data.style === "checklist" && | ||
item.meta && | ||
item.meta.hasOwnProperty("checked") | ||
) { | ||
const checked = item.meta.checked ? " checked" : ""; | ||
return `<li><label><input type="checkbox"${checked} disabled> ${content}</label>${nestedList}</li>`; | ||
} | ||
|
||
return `<li>${content}${nestedList}</li>`; | ||
}); | ||
|
||
return `<${listStyle}>${list.join("")}</${listStyle}>`; | ||
let attributes = ""; | ||
|
||
if (listStyle === "ol" && data.meta) { | ||
if (data.meta.start && data.meta.start !== 1) { | ||
attributes += ` start="${data.meta.start}"`; | ||
} | ||
if (data.meta.counterType && data.meta.counterType !== "numeric") { | ||
attributes += ` type="${getCounterTypeAttribute(data.meta.counterType)}"`; | ||
} | ||
} | ||
|
||
return `<${listStyle}${attributes}>${list.join("")}</${listStyle}>`; | ||
}; | ||
|
||
return recursor(data.items, listStyle); | ||
}; | ||
|
||
function getCounterTypeAttribute(counterType: string): string { | ||
switch (counterType) { | ||
case "lower-alpha": | ||
return "a"; | ||
case "upper-alpha": | ||
return "A"; | ||
case "lower-roman": | ||
return "i"; | ||
case "upper-roman": | ||
return "I"; | ||
default: | ||
return "1"; | ||
} | ||
} |
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.
Hey @blade47
Can you update this to just return an image? this might be the best option for emails but the purpose of the lib is to simply return html and with existing projects using this would break the functionality.
Also, you could add any styling using css. Ideally, the image element should just return image. On the other end, I am okay with you adding a custom parser which can then be plugged in the library constructor.