Skip to content

[IMP] extensions: add copy to markdown button #13998

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

Open
wants to merge 1 commit into
base: 18.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions extensions/odoo_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def setup(app):
app.add_js_file('js/menu.js')
app.add_js_file('js/page_toc.js')
app.add_js_file('js/switchers.js')
app.add_js_file('js/turndown.js')
app.add_js_file('js/copy_page_md.js')

roles.register_canonical_role('icon', icon_role)

Expand Down
5 changes: 5 additions & 0 deletions extensions/odoo_theme/layout_templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
<div class="d-flex justify-content-end ms-auto">
{%- include "layout_templates/language_switcher.html" %}
{%- include "layout_templates/version_switcher.html" %}
<div class="ms-2">
<button id="o_copy_markdown" class="btn btn-sm" type="button" title="{{ _('Copy page as Markdown') }}">
<i class="fa fa-copy"></i>
</button>
</div>
</div>
</div>
46 changes: 46 additions & 0 deletions extensions/odoo_theme/static/js/copy_page_md.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(function () {
document.addEventListener('DOMContentLoaded', () => {
const btn = document.getElementById('o_copy_markdown');
const content = document.getElementById('o_content');
if (!btn || !content || typeof TurndownService === 'undefined') return;

const originalHtml = btn.innerHTML;
const baseUrl = window.location.href;
const ATTRS = ['src', 'href', 'poster', 'data', 'action', 'formaction'];

btn.addEventListener('click', () => {
content.querySelectorAll('.headerlink, .modal img').forEach(el => el.remove());

const td = new TurndownService({ headingStyle: 'atx', bulletListMarker: '-' });

td.addRule('code', {
filter: n => n.nodeName === 'DIV' && n.className.includes('highlight') && n.querySelector('pre'),
replacement: (_, n) => {
const lang = n.className.match(/highlight-(\w+)/)?.[1] || '';
const code = n.querySelector('pre').textContent.trim();
return `\n\`\`\`${lang}\n${code}\n\`\`\`\n`;
}
});

let html = content.innerHTML;
try {
const doc = new DOMParser().parseFromString(html, 'text/html');
ATTRS.forEach(attr => {
doc.querySelectorAll(`[${attr}]`).forEach(el => {
const val = el.getAttribute(attr);
if (val && !/^https?:|^data:|^#/.test(val)) {
el.setAttribute(attr, new URL(val, baseUrl).href);
}
});
});
html = doc.body.innerHTML;
} catch (e) {}

const markdown = td.turndown(html);
navigator.clipboard.writeText(markdown).then(() => {
btn.innerHTML = 'Copied!';
setTimeout(() => (btn.innerHTML = originalHtml), 2000);
});
});
});
})();
Loading