mdBook icon indicating copy to clipboard operation
mdBook copied to clipboard

Copy button copies incorrect code to clipboard

Open mtn opened this issue 7 years ago • 3 comments

This issue originally came up here.

It seems like in some cases, the "Copy to clipboard button" copies incorrect code to the clipboard. For example (from that issue), we get this:


#![allow(unused_variables)]
#![feature(panic_implementation)]
#![feature(core_intrinsics)]
#![no_std]
#![no_main]

fn main() {
use core::intrinsics;
use core::panic::PanicInfo;

#[panic_implementation]
#[no_mangle]
fn panic(_info: &PanicInfo) -> ! {
    unsafe { intrinsics::abort() }
}

#[no_mangle]
pub fn _start() -> ! {
    loop {}
}
}

where we'd expect this:

#![feature(panic_implementation)]
#![feature(core_intrinsics)]
#![no_std]
#![no_main]

use core::intrinsics;
use core::panic::PanicInfo;

#[panic_implementation]
#[no_mangle]
fn panic(_info: &PanicInfo) -> ! {
    unsafe { intrinsics::abort() }
}

#[no_mangle]
pub fn _start() -> ! {
    loop {}
}

Edit: I guess it has something to do with a number of span class="hidden"s being created. What's the motivation behind these?

mtn avatar Jun 20 '18 16:06 mtn

I guess it has something to do with a number of span class="hidden"s being created. What's the motivation behind these?

This happens due to https://doc.rust-lang.org/stable/rustdoc/documentation-tests.html#pre-processing-examples and https://doc.rust-lang.org/stable/rustdoc/documentation-tests.html#hiding-portions-of-the-example ; mdbook does this just like rustdoc does.

That said, I think when we ignore, we shouldn't do this, which is the root of this issue. That is, whenever this logic is not useful, we shouldn't add it. We currently always add it, but that only matters for stuff that's rust.

steveklabnik avatar Jun 20 '18 21:06 steveklabnik

would it be feasible to add a configuration option on the mdbook level to control whether the main gets added or not?

iolivia avatar Jul 13 '20 19:07 iolivia

fixed it here https://github.com/rust-lang/mdBook/pull/1911#issue-1409974442

yoyomo avatar Oct 17 '22 18:10 yoyomo