From 0af79cd3a5149fc815ed420c9b1a58fda1adb0d4 Mon Sep 17 00:00:00 2001 From: ketankr9 Date: Mon, 10 Feb 2020 17:16:47 +0530 Subject: [PATCH] remove html tag shown in problem (in terminal) fixes https://github.com/skygragon/leetcode-cli/issues/201 & https://github.com/skygragon/leetcode-cli/issues/183 --- lib/commands/show.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/commands/show.js b/lib/commands/show.js index 56b9341e..a256ea21 100644 --- a/lib/commands/show.js +++ b/lib/commands/show.js @@ -1,6 +1,8 @@ 'use strict'; var util = require('util'); +var cheerio = require('cheerio'); +var he = require('he'); var _ = require('underscore'); var childProcess = require('child_process'); @@ -89,6 +91,19 @@ function genFileName(problem, opts) { } } +// removes html tags from problem description before printing it to the terminal +function cleanProblemDesc(desc){ + // Replace with '^' as the power operator + desc = desc.replace(/<\/sup>/gm, '').replace(//gm, '^'); + desc = he.decode(cheerio.load(desc).root().text()); + // NOTE: wordwrap internally uses '\n' as EOL, so here we have to + // remove all '\r' in the raw string. + desc = desc.replace(/\r\n/g, '\n').replace(/^ /mg, '⁠'); + // const wrap = require('wordwrap')(79); + // desc = wrap(desc); + return desc; +} + function showProblem(problem, argv) { const taglist = [problem.category] .concat(problem.companies || []) @@ -168,7 +183,7 @@ function showProblem(problem, argv) { log.printf('* Source Code: %s', chalk.yellow.underline(filename)); log.info(); - log.info(problem.desc); + log.info(cleanProblemDesc(problem.desc)); } cmd.handler = function(argv) {