question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for color property

See original GitHub issue

It seems that the color property could not convert from docx. <span style="#ff0000">red</span> or <font color="#ff0000">red</font> But just found another library that fork from you could.(https://github.com/sbabushkin/mammoth-colors.js) Thanks for sharing this amazing library. 😍

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
bigiCrabcommented, Apr 13, 2020

By imitate @mahendra119 solution, found out how to convert highlight(bg-color, I think is same)

btw, in my workflow i dont want to convert css by hand. so i made some change for conver color.

mammoth/lib/documents.js in function Run()

color: properties.color ? '#' + properties.color : null,
highlight: properties.highlight ? properties.highlight : null

mammoth/lib/document-to-html.js in function convertRun()

if (run.color) {
    paths.push(htmlPaths.element('font', {color : run.color}, {fresh: false}));
}
if (run.highlight) {
// i think set attr by style might cause some issue.
    paths.push(htmlPaths.element('mark', {'style' : 'background-color:' + run.highlight}, {fresh: false}));
}

mammoth/lib/docx/body-reader.js in function readRunProperties()

color: element.firstOrEmpty("w:color").attributes["w:val"],
highlight: element.firstOrEmpty("w:highlight").attributes["w:val"],
7reactions
mahendra119commented, Feb 10, 2020

Plugin Maker did not provided any text color parsing. but i have changed some code inside this package to get color parsing work. I am using version 1.4.6 go to mammoth/lib/documents.js and update this code function Run(children, properties) { properties = properties || {}; return { type: types.run, children: children, styleId: properties.styleId || null, styleName: properties.styleName || null, isBold: properties.isBold, isUnderline: properties.isUnderline, isItalic: properties.isItalic, isStrikethrough: properties.isStrikethrough, isSmallCaps: properties.isSmallCaps, verticalAlignment: properties.verticalAlignment || verticalAlignment.baseline, font: properties.font || null, color: properties.color }; } I have added only last line for color property

go to mammoth/lib/document-to-html.js and find convertRun function and add these lines- if (run.color) { paths.push(htmlPaths.element(‘span’, {color : run.color}, {fresh: false})); } i have added span tag here but you can add any tag.

Next, go to mammoth/lib/docx/body-reader.js and find readRunProperties function and add this line- color: element.firstOrEmpty(“w:color”).attributes[“w:val”]

And that’s it. you will get final result like this after parsing <span color="FF0000">Hello</span> but we know this is not valid so we need to convert it in valid CSS like below (result).replace(/color="/g, ‘style="color:#’);

I didn’t got any support for background-color. hopefully plugin maker will provide support for this in next version and give an option whether user want to turn on color parsing or not, instead of disabling by default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

color - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
The color CSS property sets the foreground color value of an element's text and text decorations, and sets the currentcolor value.
Read more >
CSS property: color-scheme | Can I use... Support ... - CanIUse
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web ... CSS property: color-scheme....
Read more >
CSS color property - W3Schools
The color property specifies the color of text. Tip: Use a background color combined with a text color that makes the text easy...
Read more >
CSS/Properties/color/keywords - W3C Wiki
Color name Hex rgb Decimal aliceblue #f0f8ff 240,248,255 antiquewhite #faebd7 250,235,215 aqua #00ffff 0,255,255
Read more >
Improved dark mode default styling with the color-scheme ...
The color-scheme CSS property and the corresponding meta tag allow developers to opt their pages in to theme-specific defaults of the user ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found