2.1.0 • Published 1 year ago

@momsfriendlydevco/embed-gdocs v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@MomsFriendlyDevCo/Embed-GDocs

Simple, agnostic component to embed a Google Doc into a webpage.

Features:

  • Simple API to include a Google-Doc embed within any webpage
  • Platform and framework agnostic - works as plain JS with no dependencies
  • Included fixes for common issues with embeds (various style fixes, unminify links, retarget links to open in other tags)
<html>
<head>
	<title>@MomsFriendlyDevCo/Embed-GDocs Example</title>
	<script src="/dist/embed-gdocs.js"></script>
	<script type="module">
	import embedGDoc from '/dist/embed-gdocs.js';

	window.addEventListener('load', ()=> {
		embedGDoc({
			selector: '#gdoc',
			url: 'https://docs.google.com/document/d/e/2PACX-1vTasmjm8_rI_tzzzMs0xl5AhjafHXCPs33uyq6VShbMepnlkumS9rDBkxbEs0AAoAtdMRm-dmoGXxbR/pub?embedded=true',
		});
	});
	</script>
</head>
<body>
	<div id="gdoc"></div>
</body>
</html>

API

embedGDoc(url, options)

import embedGDoc from '@momsfriendlydevco/embed-gdoc';

Insert a Google document within a given element.

Options are:

OptionTypeDefaultDescription
selectorString / HTMLElementEither the DOM node to replace or a selector to use
urlStringThe Google Docs published URL to embed - this generally ends in /pub?embedded=true
urlOptionsObjectAdditional Fetch options when retrieving the document from the url
keepStyleBooleantrueKeep the source document style, if false this removes the style completely
fixContentTrimBooleantrueRemove the outer wrapping of the element and just use the embedded content
fixWidthBooleantrueRemove page width restrictions
fixParaMarginsBooleantrueAdd slight margin to paragraphs
fixTableWidthBooleantrueRemove table width restrictions
fixPaddingBooleantrueRemove page padding
fixLinkTargetsBooleantrueMake all links open in a new tab instead of replacing the current one
fixLinkShortenBooleantrueRemove Google tracking URL prefix from links
fixImageTitleAsLinkBooleantrueIf an image "alternative text" (actually the title attribute) looks like a link make the image linkable - this is to fix how Google Docs weirdly handles image linking
onLoadFunctionhtml => htmlCalled as (html:String) when the HTML has been loaded, expected to return the mutated input
onMountFunctionel => nullCalled as (el:DomElement) when the Dom element has been created but has not yet been added into the DOM, can mutate the input element

clean(html, options)

import {clean} from '@momsfriendlydevco/embed-gdoc';
// OR
import clean from '@momsfriendlydevco/embed-gdoc/clean';

Take input HTML and return a "clean" version of the same fixing various issues. This is the actual worker of the other supplied functions.

Options are inherited from embedGDoc() but also include:

OptionTypeDefaultDescription
createElementFunctiondocument.createElementHow to create DOM fragments

html(html, options)

import {html} from '@momsfriendlydevco/embed-gdoc';
// OR
import html from '@momsfriendlydevco/embed-gdoc/html';

Similar to embedGDoc() but return only the resulting HTML. This function is used mainly for backend scripts which wish to clean up the resulting HTML before serving it directly.

Options are inherited from embedGDoc() + clean().

2.1.0

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.3

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago