Skip to content

Commit

Permalink
[IMP] web_widget_html_markdown: bidirectional editing
Browse files Browse the repository at this point in the history
Possible to use markdown for html fields, or html editor and
switch between the two.
  • Loading branch information
NL66278 committed Sep 12, 2023
1 parent b5a08d7 commit 4ffa0a3
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 290 deletions.
23 changes: 12 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ repos:
entry: found forbidden files; remove them
language: fail
files: "\\.rej$"
- id: en-po-files
name: en.po files cannot exist
entry: found a en.po file
language: fail
files: '[a-zA-Z0-9_]*/i18n/en\.po$'
- repo: https://github.com/oca/maintainer-tools
rev: ab1d7f6
hooks:
Expand Down Expand Up @@ -96,15 +101,15 @@ repos:
- id: pyupgrade
args: ["--keep-percent-format"]
- repo: https://github.com/PyCQA/isort
rev: 5.5.1
rev: 5.12.0
hooks:
- id: isort
name: isort except __init__.py
args:
- --settings=.
exclude: /__init__\.py$
- repo: https://github.com/acsone/setuptools-odoo
rev: 2.6.0
rev: 3.1.8
hooks:
- id: setuptools-odoo-make-default
- id: setuptools-odoo-get-requirements
Expand All @@ -113,25 +118,21 @@ repos:
- requirements.txt
- --header
- "# generated from manifests external_dependencies"
- repo: https://gitlab.com/PyCQA/flake8
- repo: https://github.com/PyCQA/flake8
rev: 3.8.3
hooks:
- id: flake8
name: flake8
additional_dependencies: ["flake8-bugbear==20.1.4"]
- repo: https://github.com/PyCQA/pylint
rev: v2.11.1
- repo: https://github.com/OCA/pylint-odoo
rev: 7.0.2
hooks:
- id: pylint
- id: pylint_odoo
name: pylint with optional checks
args:
- --rcfile=.pylintrc
- --exit-zero
verbose: true
additional_dependencies: &pylint_deps
- pylint-odoo==5.0.5
- id: pylint
name: pylint with mandatory checks
- id: pylint_odoo
args:
- --rcfile=.pylintrc-mandatory
additional_dependencies: *pylint_deps
47 changes: 47 additions & 0 deletions web_widget_html_markdown/doc/tech_design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Technical design of the markdown for html fields widget

When this widget is linked to an html field, the user will be able to switch between an
html editor and a markdown editor for the fields content. The display of the field, and
the contents stored will always be html.

Actually when editing in markdown, this markdown will also be stored in html, but then
as a hidden code section. In this way using the widget does not need any new database
element.

To convert from markdown to html, the marked library is used: [marked]:
https://github.com/markedjs/marked

To convert from html to markdown the turndown library is used: [turndown]:
https://github.com/mixmark-io/turndown

## Events

1. Starting the editor

When a field is going to be edited, we have to show either the html editor or the
markdown editor. For existing records, we just look at the presence or absence of stored
markdown code. For new records we will look at the field options.

2. Switching to markdown

- Take the html value and convert it to markdown;
- Create the markdown hidden paragraph, and add it to the html;
- Activate the markdown editor;
- Change to info in the control area to show markdown is being used.

3. Switching to html

- Take the markdown content and convert it to html;
- Remove the hidden markdown paragraph;
- Activate the html editor;
- Change to info in the control area to show html is being used.

4. Saving the form: commiting the changes

If html is being used, nothing special needs to be done.

If markdown is being used:

- Convert the markdown value to html;
- Add the hidden paragraph with the markdown content;
- Replace the html contents with the html genererated, including the hidden paragraph.
1 change: 1 addition & 0 deletions web_widget_html_markdown/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Tom Blauwendraat <[email protected]>
* Kevin Kamau <[email protected]>
* Giovanni Francesco Capalbo <[email protected]>
* Ronald Portier <[email protected]>
18 changes: 12 additions & 6 deletions web_widget_html_markdown/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
Behaviour of the module and best practices
==========================================

This module implements a Markdown editor on Html fields, in contrast to web_widget_text_markdown, which implements it on Text fields. In readonly mode, the widget displays HTML, but when editing, the widget offers you an option to edit in Markdown or in HTML. If you edit markdown, it will save as the rendered HTML, but with the source Markdown embedded inside a <script> tag. When editing again, it will show you the Markdown source. If you edit HTML, you will lose the Markdown and the content will just behave as a regular HTML field with an HTML widget.
This module implements a markdown editor on html fields, in contrast to
web_widget_text_markdown, which implements it on text fields. In readonly mode,
the widget displays html, but when editing, the widget offers you an option to edit
in markdown or in html. If you edit markdown, it will save as the rendered html,
but with the source markdown embedded inside a <script> tag. When editing again,
it will show you the markdown source. If you edit html, you will lose the markdown
and the content will just behave as a regular html field with an html widget.

Utility of this module vs web_widget_text_markdown
==================================================
The benefit of this module over web_widget_text_html is that it allows markdown-editing of HTML fields such as for example the mail.message body for the chatter, or HTML fields that end up in printed reports such as the Quotation description. Such fields cannot be converted to Text fields because it will cause problems for their functionality. But with this widget, they can still be edited as Markdown.
Limitations
===========

A difficulty with this module is that once you start editing a field as HTML, whether with this Markdown widget in HTML mode, or through a view that has the normal HTML editor, you will lose the Markdown source and you will have to keep editing the field as HTML, or do a backconversion to Markdown. This backconversion is always lossy to a certain extent and we did not bother implementing it - but it's something for the roadmap.
.
The module does allow switching from html to markdown editing. However the conversion
from html to markdown will most likely lose some information. That is why users
are warned before making this switch.
3 changes: 2 additions & 1 deletion web_widget_html_markdown/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* Implement conversion from HTML to Markdown, either through a JS or a Python module, instead of not allowing it, or allowing it only after HTML is emptied.
* Create a config parameter switch such as this that will enable a markdown option
for html fields throughout the whole system.
29 changes: 15 additions & 14 deletions web_widget_html_markdown/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ Your XML form view definition should contain:
This will replace the default Html widget with the new Markdown/HTML widget and
allow the field to be edited as Markdown or HTML, depending on the user's choice.

#### Options
- default_markdown_on_new : if true when creating a new record, markdown will
be selected by default.
- only_markdown_on_new : if true when creating a new record, only markdown
will be present on radiobutton.
#### Options
- default_markdown_on_new : if true when creating a new record, markdown will
be selected by default.
- only_markdown_on_new : if true when creating a new record, only markdown
will be present on radiobutton.

example
example

´´´
<field name="description"
widget="html_markdown"
options="{'only_markdown_on_new' : 1,
'default_markdown_on_new':1}"/>
´´´
´´´
<field
name="description"
widget="html_markdown"
options="{'only_markdown_on_new' : 1, 'default_markdown_on_new':1}"
/>
´´´

will offer only markdown on new records , very useful for exqmple in mail threads,
if we want to have markdown only threads.
will offer only markdown on new records , very useful for example in mail threads,
if we want to have markdown only threads.
Loading

0 comments on commit 4ffa0a3

Please sign in to comment.