Skip to content

Instantly share code, notes, and snippets.

@pspaul
Last active June 3, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pspaul/50cea9b9ec18a00070fb0990543bbd0c to your computer and use it in GitHub Desktop.
Save pspaul/50cea9b9ec18a00070fb0990543bbd0c to your computer and use it in GitHub Desktop.
// moodle2md
var md = '';
document.querySelectorAll('#responseform > div > div.que').forEach(q => {
if (q.classList.contains('description')) {
return;
}
md += '## ' + q.querySelector('.info > h3').innerText.trim() + '\n';
if (q.classList.contains('multichoice')) {
md += '> ' + q.querySelector('.qtext').innerText.trim().replace(/\n/g, ' \n> ') + '\n\n';
q.querySelectorAll('.answer > div').forEach(answer => {
md += '- [' + (answer.querySelector('input:last-of-type').checked ? 'x' : ' ') + '] ';
md += answer.querySelector('label').innerText.trim();
md += '\n'
});
} else if (q.classList.contains('numerical') || q.classList.contains('shortanswer')) {
md += '> ' + q.querySelector('.qtext').innerText.trim().replace(/\n/g, ' \n> ') + '\n\n';
md += q.querySelector('.answer > input').value.trim() || '*nicht beantwortet*';
md += '\n';
} else if (q.classList.contains('multianswer')) {
md += '> ' + q.querySelector('.formulation > p:nth-of-type(1)').innerText.trim().replace(/\n/g, '\n> ') + '\n\n'
q.querySelectorAll('.formulation > p:nth-of-type(n + 3)').forEach(answer => {
if (answer.childNodes[0].nodeValue.trim() === '') {
return;
}
md += answer.childNodes[0].nodeValue.trim() + ' ';
md += answer.querySelector('input').value.trim() || '*nicht beantwortet*';
md += ' \n';
});
}
md += '\n';
});
console.log(md);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment