Repository navigation
How to convert Delta JSON to Markdown using server-side Python #4828
Replies: 2 comments 1 reply
|
Here is the same thing, but to html. def quill_to_html(json, path_do_arquivo):
string_final = ""
with path_do_arquivo.open("r", encoding="utf-8") as file:
rawChapterData = file.read()
jsonData = json.loads(rawChapterData)
lista = jsonData["ops"]
tamanho_lista = len(lista)
i = 0
while i < tamanho_lista:
# Caso seja o último elemento
if i + 1 >= tamanho_lista:
string_final += lista[i]["insert"]
break
texto = lista[i]["insert"]
proximo = lista[i + 1]
atributos = proximo.get("attributes", {})
# HEADER
if "header" in atributos:
if atributos["header"] == 1:
primeiro_elemento = "<h1>"
segundo_elemento = "</h1>"
elif atributos["header"] == 2:
primeiro_elemento = "<h2>"
segundo_elemento = "</h2>"
elif atributos["header"] == 3:
primeiro_elemento = "<h3>"
segundo_elemento = "</h3>"
else:
primeiro_elemento = ""
segundo_elemento = ""
string_final += (
primeiro_elemento
+ texto
+ segundo_elemento
+ proximo["insert"]
)
# LISTA
elif "list" in atributos:
tipo_lista = atributos["list"]
# Texto antes da última quebra de linha
temp = texto.split("\n")
if len(temp) > 1:
item_da_lista = temp[-1]
temp.pop()
intermediario = "\n".join(temp)
if intermediario:
string_final += intermediario + "\n"
else:
item_da_lista = texto
if tipo_lista == "ordered":
string_final += "<ol><li>" + item_da_lista + "</li></ol>"
elif tipo_lista == "bullet":
string_final += "<ul><li>" + item_da_lista + "</li></ul>"
string_final += proximo["insert"]
# BOLD
elif "bold" in atributos:
string_final += (
texto
+ "<strong>"
+ proximo["insert"]
+ "</strong>"
)
# ITALIC
elif "italic" in atributos:
string_final += (
texto
+ "<em>"
+ proximo["insert"]
+ "</em>"
)
# UNDERLINE
elif "underline" in atributos:
string_final += (
texto
+ "<ins>"
+ proximo["insert"]
+ "</ins>"
)
# LINK
elif "link" in atributos:
link = atributos["link"]
string_final += (
texto
+ f"<a href='{link}'>"
+ proximo["insert"]
+ "</a>"
)
# Nenhum atributo
else:
string_final += texto + proximo["insert"]
i += 2
# Quebras de linha do Quill para HTML
string_final = string_final.replace("\n", "<br>\n")
return string_finalCan be very usefull as an intermediary for exporting to other formats. |
|
For text inserts, each op is a run, rather than half of a pair. For example, this is one paragraph with three runs: {"ops":[{"insert":"A "},{"insert":"bold","attributes":{"bold":true}},{"insert":" ending\n"}]}
I'd accumulate text runs until each newline, keeping each run's inline attributes, then choose the block format from that newline's There is a runnable Python file-export example, sample chapter and tests here. It uses only the standard library and covers the default Snow toolbar's text formats, including mixed and nested lists. Unsupported formats and embeds fail explicitly before replacing an existing export. The example uses Markdown blocks with inline HTML for styled text. Underline and explicit empty paragraphs therefore need a Markdown renderer that retains HTML. Checks include real Quill 2.0.3 Use the saved complete |
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone!
I'm building an open-source text editor for writers using Quill. I store my editor content locally as Quill Delta JSON files, and I've been working on a Python script to convert these Deltas directly into Markdown files.
Since Delta ops represent line formats (like headers or lists) on the trailing newline character, parsing them sequentially in Python required a bit of tweaking.
Here is my current implementation handling basic inline styling, headers, lists, and links:
It works well for basic formatting (images are not supported yet).
I wanted to share this here for anyone working with Quill Deltas on a Python backend/tooling setup, and to ask:
Any feedback or suggestions are welcome!
All reactions