Skip to content
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

Save Paste button should save to .md extension if paste format is markdown #1293

Open
nain-F49FF806 opened this issue Apr 28, 2024 · 7 comments
Assignees
Labels
enhancement good first issue Contributors could start with this easy task. help wanted

Comments

@nain-F49FF806
Copy link

The problem

I recently opened a paste that was marked markdown, like this one.
When clicking on save paste button, I found it cool that it downloaded a file automatically with the pastes id as name. But the extension was .txt.

The solution

For regular plaintext pastes the above makes sense, but since the paste format was Markdown, and is also being displayed as markdown, maybe it'd be more intuitive and seamless if the download is also with extension .md ?

@elrido elrido added help wanted good first issue Contributors could start with this easy task. labels Apr 29, 2024
@9401adarsh
Copy link
Contributor

9401adarsh commented May 6, 2024

Hey there, can I take up this issue ? Would appreciate it if someone could lend a few pointers on where to begin.

@elrido
Copy link
Contributor

elrido commented May 6, 2024

Sure thing, go for it. It should not be too complicated, the filename is the first thing generated in the TopNav.downloadText method:

PrivateBin/js/privatebin.js

Lines 3825 to 3834 in 28f3e54

/**
* download text
*
* @name TopNav.downloadText
* @private
* @function
*/
function downloadText()
{
var filename='paste-' + Model.getPasteId() + '.txt';

You could look up the current format using the PasteViewer.getFormat method - you would look for it to be markdown to change the suffix to .md.

@9401adarsh
Copy link
Contributor

Thanks for the pointers, will keep you posted of the progress here.

@9401adarsh
Copy link
Contributor

9401adarsh commented May 20, 2024

Hello, sorry wasn't able to get back in the last couple of weeks. I have made the changes to the privatebin.js file as per your suggestion, and have run the unit tests.

Modified downloadText() method:

        function downloadText()
        {
            var fileFormat = PasteViewer.getFormat() === 'markdown' ? '.md' : '.txt';
            var filename = 'paste-' + Model.getPasteId() + fileFormat;
            var text = PasteViewer.getText();

            var element = document.createElement('a');
            element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
            element.setAttribute('download', filename);

            element.style.display = 'none';
            document.body.appendChild(element);

            element.click();

            document.body.removeChild(element);
        }
        

Unit Test Log:

PHPUnit 10.5.20 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.18
Configuration: /home/adarsh9401/Desktop/Dev/PrivateBin/tst/phpunit.xml

D..............................................................  63 / 369 ( 17%)
............................................................... 126 / 369 ( 34%)
............................................................... 189 / 369 ( 51%)
............................................................... 252 / 369 ( 68%)
........................DDDDDD................................. 315 / 369 ( 85%)
......................................................          369 / 369 (100%)

Time: 00:31.538, Memory: 20.00 MB

There were 2 PHPUnit test runner warnings:

1) Class Bootstrap cannot be found in /home/adarsh9401/Desktop/Dev/PrivateBin/tst/Bootstrap.php

2) XDEBUG_MODE=coverage or xdebug.mode=coverage has to be set

--

There was 1 PHPUnit test runner deprecation:

1) Your XML configuration validates against a deprecated schema. Migrate your XML configuration using "--migrate-configuration"!

--

1 test triggered 1 PHPUnit deprecation:

1) JsonApiTest::testShortenViaYourls
Data Provider method JsonApiTest::baseUriProvider() is not static

/home/adarsh9401/Desktop/Dev/PrivateBin/tst/JsonApiTest.php:290

WARNINGS!
Tests: 369, Assertions: 6084, Warnings: 2, Deprecations: 3.


Screenshot to show that file is downloaded as .md:

image

Is this good to go ?

@rugk
Copy link
Member

rugk commented May 20, 2024

Sure, if it is good create a pull request and we can review it and suggest changes, if needed.

BTW, you can also mark code snippets with the language, which adds syntax highlighting to them, in the Markdown you write on GitHub.

@9401adarsh
Copy link
Contributor

Will create a pull request from my feature branch of the forked repo to the master branch of the official repo. Hope that works ?

@rugk
Copy link
Member

rugk commented May 20, 2024

Sure, it should, just do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue Contributors could start with this easy task. help wanted
Projects
None yet
Development

No branches or pull requests

4 participants