Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/src/bundles/webapp/readme-rendering.js
/** | /** | ||||
* Copyright (C) 2018 The Software Heritage developers | * Copyright (C) 2018 The Software Heritage developers | ||||
* See the AUTHORS file at the top-level directory of this distribution | * See the AUTHORS file at the top-level directory of this distribution | ||||
* License: GNU Affero General Public License version 3, or any later version | * License: GNU Affero General Public License version 3, or any later version | ||||
* See top-level LICENSE file for more information | * See top-level LICENSE file for more information | ||||
*/ | */ | ||||
import {handleFetchError} from 'utils/functions'; | import {handleFetchError} from 'utils/functions'; | ||||
export async function renderMarkdown(domElt, markdownDocUrl) { | export async function renderMarkdown(domElt, markdownDocUrl) { | ||||
let showdown = await import(/* webpackChunkName: "showdown" */ 'utils/showdown'); | let showdown = await import(/* webpackChunkName: "showdown" */ 'utils/showdown'); | ||||
let xssFilter = require('showdown-xss-filter'); | |||||
$(document).ready(() => { | $(document).ready(() => { | ||||
let converter = new showdown.Converter({tables: true}); | let converter = new showdown.Converter({tables: true, extensions: [xssFilter]}); | ||||
fetch(markdownDocUrl) | fetch(markdownDocUrl) | ||||
.then(handleFetchError) | .then(handleFetchError) | ||||
.then(response => response.text()) | .then(response => response.text()) | ||||
.then(data => { | .then(data => { | ||||
$(domElt).addClass('swh-showdown'); | $(domElt).addClass('swh-showdown'); | ||||
$(domElt).html(converter.makeHtml(data)); | $(domElt).html(converter.makeHtml(data)); | ||||
}) | }) | ||||
.catch(() => { | .catch(() => { | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | fetch(txtDocUrl) | ||||
.then(handleFetchError) | .then(handleFetchError) | ||||
.then(response => response.text()) | .then(response => response.text()) | ||||
.then(data => { | .then(data => { | ||||
let orgMode = '-*- mode: org -*-'; | let orgMode = '-*- mode: org -*-'; | ||||
if (data.indexOf(orgMode) !== -1) { | if (data.indexOf(orgMode) !== -1) { | ||||
renderOrgData(domElt, data.replace(orgMode, '')); | renderOrgData(domElt, data.replace(orgMode, '')); | ||||
} else { | } else { | ||||
$(domElt).addClass('swh-readme-txt'); | $(domElt).addClass('swh-readme-txt'); | ||||
$(domElt).html(`<pre>${data}</pre>`); | $(domElt) | ||||
.html('') | |||||
.append($('<pre></pre>').text(data)); | |||||
} | } | ||||
}) | }) | ||||
.catch(() => { | .catch(() => { | ||||
$(domElt).text('Readme bytes are not available'); | $(domElt).text('Readme bytes are not available'); | ||||
}); | }); | ||||
}); | }); | ||||
} | } |