Markdown Preview in txti

When EDITING a txti (txti.es/*/edit) in txti.es you should be able to preview your changes (a toggle preview button appears)

  1. // ==UserScript==
  2. // @name Markdown Preview in txti
  3. // @namespace https://gist.github.com/AeonFr/f44a8b6f175d059f8328638a6a30ce4d
  4. // @version 0.1
  5. // @description When EDITING a txti (txti.es/*/edit) in txti.es you should be able to preview your changes (a toggle preview button appears)
  6. // @author Francisco Cano
  7. // @match http://txti.es/*/edit
  8. // @grant none
  9. // @run-at document-end
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/showdown/1.3.0/showdown.min.js
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. function _(selector){
  16. return document.querySelector(selector);
  17. }
  18. window.addEventListener('load', function(){
  19. initMarkdownPreview();
  20. });
  21. var converter = false;
  22. function initMarkdownPreview(){
  23. if( _('#content-input') ){
  24. var elt = document.createElement('div'),
  25. converter = new showdown.Converter();
  26. elt.id = 'content-preview';
  27. elt.style.display = 'none';
  28. _('#create-a-txti').insertBefore(elt, _('#content-input'));
  29. var contentToMarkdownPreviewer = function(){
  30. var result = converter.makeHtml( _('#content-input').value );
  31. elt.innerHTML = result;
  32. };
  33. _('#content-input').addEventListener('keyup', contentToMarkdownPreviewer);
  34. // Fire the conversion the first time, when page is loaded
  35. contentToMarkdownPreviewer();
  36. var elt2 = document.createElement('div');
  37. elt2.innerHTML = '<a href="content-input" action="toggle-preview">Toggle Preview</a>';
  38. _('#create-a-txti').insertBefore(elt2, _('#content-preview'));
  39. elt2.addEventListener('click', function(e){
  40. e.preventDefault();
  41. if ( _('#content-input').style.display != 'none' ){
  42. _('#content-input').style.display = 'none';
  43. _('#content-preview').style.display = 'block';
  44. }else{
  45. _('#content-input').style.display = 'block';
  46. _('#content-preview').style.display = 'none';
  47. }
  48. });
  49. }
  50. }
  51. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址